0

I'm getting this error in sell.php -

Notice: Undefined index: shares in /home/jharvard/vhosts/pset7/public/sell.php on line 20

My code :-

<?php

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

  if($_SERVER["REQUEST_METHOD"] == "GET")
  {

    $symbols = query("SELECT * FROM share WHERE id = ?", $_SESSION["id"]);
    render("sell_form.php",["symbols" => $symbols]);
  }

  else if($_SERVER["REQUEST_METHOD"] == "POST")
  {

    $stocka = query("SELECT * FROM share WHERE id = ? AND symbol = ?",$_SESSION["id"],$_POST["symbol"]);

    $stockcur = lookup($_POST["symbol"]);

    $addcash = $stockcur["price"] * $stocka["shares"];



    query("DELETE FROM share WHERE id = ? AND symbol = ?",$_SESSION["id"],$_POST["symbol"]);

    query("UPDATE users SET cash = cash + ? WHERE id = ?",$addcash,$_SESSION["id"]);


  }

?>

But shares is there in the share table, so I dont understand what's wrong.

1
  • It would help if you would identify which line is line 20. You need to also understand that an INDEX is not a table and it is not a column. It is a structure that allows for rapid searches of a table based on the contents of one or more columns. You have a query that is looking for an index. Finally, did the error say undefined index 'shares' or 'share'? Given the usage of both in this pset, a misspelling of share vs. shares is important. – Cliff B Nov 21 '15 at 17:15
2

the notice tells you what exactly the problem is -- there's no index called "shares" in stocka. recall that query returns an array of rows, even if the result was a single row, in which case it would be in the zeroth index.

2
  • 1
    "A man with a hammer sees everything as a nail." I'm a database guy so I was incorrectly thinking database table index. Kareem is right. In this case, 'index' is the name of the variable in the associative pair, much like a number is the index in a c array. Suggest you use the dump function to dump the contents of $stocka. It'll tell you both the index names and associated values. It will also tell you how many rows you're getting and the structure. ( I'm also thinking that you might need to account for the row before looking for "shares", as in a for each loop, even if it's only 1 row. – Cliff B Nov 21 '15 at 20:49
  • Yeah that was the problem, thanks! – Jedi18 Nov 22 '15 at 7:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .