0

I'm having a bit of trouble in sell for pset 7. My form displays and when I click submit I am redirected to the portfolio. However, the portfolio is not changed: all my stock and cash are still the same.

Do I have to use the same variable names that I used in portfolio.php and index.php to get this to work? Or do I have to iterate over each number of a specific stock to delete each one individually?

my code for sell.php:

<?php

// configuration
require("../includes/config.php"); 

//see what stock they have
$stocks = query("SELECT symbol, shares FROM user_portfolio WHERE id = ?",$_SESSION["id"]);
if($_SERVER["REQUEST_METHOD"]=="POST")
{
    //if empty
    if(empty($_POST["stock"]))
    {
        apologize("You did not select a stock to sell.");
    }
    else
    {

    //apologize("Party up in here woot woot.");
    //do a lookup for price
    $price = lookup($_POST["stock"]);
    //get number of shares
    $number = query("SELECT shares FROM user_portfolio WHERE id = ? and symbol = ?", $_SESSION["id"], $_POST["stock"]);
    //get the total value of those hsres
    $value = $price["price"]*$number["shares"];
    //update number of shares
    query("DELETE FROM user_portfolio WHERE id = ? AND symbol = ?",$_SESSION["id"], $_POST["stock"]);
    //update cash value
    query("UPDATE users SET cash = cash + ? WHERE id = ?",$value,$_SESSION["id"]);
    redirect("index.php");
    }



}

render("sell_form.php",["stocks"=>$stocks]);

?>

2
  • How are you going about trying to update the user's holdings? Mind providing your sell.php controller code, or at minimally your sql sell statement?
    – lethaljd
    Commented Dec 17, 2014 at 12:52
  • @lethaljd I added my code for sell.php
    – Kim
    Commented Dec 18, 2014 at 16:16

0

You must log in to answer this question.

Browse other questions tagged .