-4

I have a table called pack_details with 4 columns. I'm trying to insert new data into an existing table. Can somebody tell me what's wrong with my codes and why i have a parse error?

$sql_query = "UPDATE pack_details SET $delivery_date = $_POST["delivery_date"], $delivery_time = $_POST["delivery_time"]
WHERE $delivery_building = $_POST["delivery_building"]
AND $delivery_room = $_POST["delivery_room"]";
dur
  • 15,689
  • 25
  • 79
  • 125
Drickuss Merguez
  • 49
  • 1
  • 1
  • 8
  • 1
    Parse error: syntax error, unexpected '"', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) – Drickuss Merguez Apr 23 '16 at 08:47
  • 1
    You can not use double quotes inside double quotes without escaping it. Also, in a mySQL query you have to wrap string data by single quotes. Change in `SET $delivery_date = '{$_POST['delivery_date']}', ` etc... – fusion3k Apr 23 '16 at 08:53
  • Thanks @fusion3k. Now it says that i have an error in your SQL syntax at this part of my code. 'delivery_time = WHERE delivery_building = AND delivery_room =' – Drickuss Merguez Apr 23 '16 at 09:44

1 Answers1

1

Try any from below options:

 $sql_query = "UPDATE pack_details SET $delivery_date = '{$_POST['delivery_date']}', $delivery_time = '{$_POST['delivery_time']}' WHERE $delivery_building = '{$_POST['delivery_building']}' AND $delivery_room = '{$_POST['delivery_room']}'";

or

 $sql_query = "UPDATE pack_details SET delivery_date = '".$_POST["delivery_date"]."', delivery_time = '".$_POST["delivery_time"]."' WHERE delivery_building = '".$_POST["delivery_building"]."' AND delivery_room = '".$_POST["delivery_room"]."'";

Note: If field name doesn't contain $, remove $ from field name in query. For eg. "$delivery_date" should be "delivery_date"

Suggestion: Instead of using string concatenation for building, You should use bind parameters to pass value to query. It helps to prevent SQL injection as well as code look well.

Krunal
  • 317
  • 1
  • 7
  • Thanks for the advices, now it says... You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' delivery_time = WHERE delivery_building = AND delivery_room =' at line 1. what's wrong with this part of my sentence? – Drickuss Merguez Apr 23 '16 at 09:41
  • You must not have added quotes for string and date type field. Which query you have tried? – Krunal Apr 23 '16 at 09:43
  • $sql_query = "UPDATE pack_details SET delivery_date = $_POST[delivery_date], delivery_time = $_POST[delivery_time] WHERE delivery_building = $_POST[delivery_building] AND delivery_room = $_POST[delivery_room]"; – Drickuss Merguez Apr 23 '16 at 10:04
  • Try $sql_query = "UPDATE pack_details SET $delivery_date = '".$_POST["delivery_date"]."', $delivery_time = '".$_POST["delivery_time"]."' WHERE $delivery_building = '".$_POST["delivery_building"]."' AND $delivery_room = '".$_POST["delivery_room"]."'"; – Krunal Apr 23 '16 at 10:07
  • This should also work $sql_query = "UPDATE pack_details SET $delivery_date = '$_POST[\"delivery_date\"]', $delivery_time = '$_POST[\"delivery_time\"]' WHERE $delivery_building = '$_POST[\"delivery_building\"]' AND $delivery_room = '$_POST[\"delivery_room\"]'"; – Krunal Apr 23 '16 at 10:09
  • Try this: $sql_query = "UPDATE pack_details SET delivery_date = '".$_POST["delivery_date"]."', delivery_time = '".$_POST["delivery_time"]."' WHERE delivery_building = '".$_POST["delivery_building"]."' AND delivery_room = '".$_POST["delivery_room"]."'"; – Krunal Apr 23 '16 at 10:33
  • If you are still getting error after above query, do echo $sql_query and tell me what it prints. – Krunal Apr 23 '16 at 10:34
  • Yaaaaaye this last one worked!!! Thanks Krunal. However when i execute my App it doesn't update these above data inside the table – Drickuss Merguez Apr 23 '16 at 10:44
  • echo the query and tried to execute query directly in your database. Check whether such record exists or not – Krunal Apr 23 '16 at 10:50
  • @Drickuss, Your upvote would be appreciated if it worked. – Krunal Apr 23 '16 at 11:05
  • how do i echo the query and tried to execute query directly in my database, please – Drickuss Merguez Apr 23 '16 at 11:18
  • Use "echo $sql_query;" to print your sql query. And execute built query in sql command prompt or in phpMyAdmin – Krunal Apr 23 '16 at 11:31
  • i'd like to update the data only in the last row, not in the entire column – Drickuss Merguez Apr 23 '16 at 12:00