0
query("INSERT INTO stocks (id, symbol, shares) VALUES($_SESSION["id"], $_POST["symbol"], $_POST["number"]) ON DUPLICATE KEY UPDATE shares = shares + VALUES(shares)");

I am getting this error

Parse error: syntax error, unexpected '"', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /home/jharvard/vhosts/pset7/public/buy.php on line 17

thanks to Kareem i fixed it using

query("INSERT INTO stocks (id, symbol, shares) VALUES(? , ? , ?) ON DUPLICATE KEY UPDATE shares = shares + VALUES(shares)", $_SESSION["id"], $_POST["symbol"], $_POST["number"]);
4
  • you're confusing the PHP parser by having double quotes inside double quotes without escaping them.
    – kzidane
    Sep 27 '15 at 5:47
  • thank you can you post that as the answer so I can mark this question solved? also was there another way? or is this the best way? @Kareem Sep 27 '15 at 5:53
  • you could use single quotes inside double quotes or vice-versa.
    – kzidane
    Sep 27 '15 at 5:58
  • thank you please post it as an answer so I can mark it solved Sep 27 '15 at 6:16
3

As I told you on Slack:

This is the syntax that will work:

"INSERT INTO table(field1,field2,field3) VALUES (?,?,?)", $var1, $var2, $var3);

and replacing the fields and variables as appropriate.

You must log in to answer this question.

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