1

Im trying to run the following code but it is giving me a strange error..

$result = $db->query("UPDATE `items` 
          SET `item_label`= ".$title.", 
              `item_quantity`=".$quantity.", `item_price`=".$price."
          WHERE `item_id` = ".$_POST['id']);

If i remove the item_label = ".$title.", from the above code it works perfectly and successfully updates the quantity and price of the given row. e.g.

$result = $db->query("UPDATE `items` 
          SET `item_quantity`=".$quantity.",
              `item_price`=".$price." 
         WHERE `item_id` = ".$_POST['id']);

when I run the code containing the item_label section it fails to set the item_label. and it gives the following error message..

Unknown column 'Updated Text' in 'field list'

Now the "Updated Text" is the value of $title.

Im baffled as to how / why it is viewing this content as a column header!?

any ideas as to why this would happen?

2
  • 5
    (1) If you print out the SQL after parameter substitution, the error is obvious 95% of the time. (2) Use parameters rather than munging the strings. (3) You probably are missing single quotes around one of the values (see (2)). Commented Mar 19, 2016 at 17:27
  • You aren't wrapping the text in any quotation marks, therefore when it executes it looks like "UPDATE items SET item_label=Updated Text, item_quantity....". Further, the benefit of using mysqli is to take advantage of bindings to prevent SQL injection attacks. See this answer for more info: stackoverflow.com/a/6514730/870729 Commented Mar 19, 2016 at 17:27

1 Answer 1

1

Since its a String you should give quotes around the $title

I would have done something like below

$result = $db->query("UPDATE items SET item_label = '".$title."', item_quantity=$quantity,item_price=$price WHERE item_id =$_POST['id']");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.