简体   繁体   中英

Unable to insert string into database

I have a json that looks like this:

{
  first_name: "ammaam",
  last_name: "mamamama",
  birth_date: "1950-1-3",
  birth_date_in_string: "03 January 1950",
  email_address: "aryalmukesh60m@gmail.com",
  ...
}

And then, I have a post request in which I send the json to a php page.

Then I have the sql that looks like this:

UPDATE users SET user_json = {"first_name":"ammaam","last_name":"mamamama","birth_date":"1950-1-3","birth_date_in_string":"03 January 1950","email_address":"aryalmukesh60m@gmail.com","password":"827ccb0eea8a706c4c34a16891f84e7b","profession":"amammamama","gender":"male","age":"68","bio":"mamamamamamammamamammamamamamammamamamamamammamamamamamamammamamamamamammamamamamammamamamamamamam","profile_picture":"..\/Images\/male_profile_picture.png","full_name":"ammaam mamamama","joined_date":"2018-04-21","joined_date_in_string":"21 April 2018","address":"","nickname":""} WHERE id = 23

All the connection related stuffs are fine and when I check the type of the variable using gettype($variable) , it shows string but I am unable to complete the query.

Can anybody spot the issue?

The query generated should have user_json value as a string. The query should be

UPDATE users SET user_json = '{"first_name":"ammaam","last_name":"mamamama","birth_date":"1950-1-3","birth_date_in_string":"03 January 1950","email_address":"aryalmukesh60m@gmail.com","password":"827ccb0eea8a706c4c34a16891f84e7b","profession":"amammamama","gender":"male","age":"68","bio":"mamamamamamammamamammamamamamammamamamamamammamamamamamamammamamamamamammamamamamammamamamamamamam","profile_picture":"..\/Images\/male_profile_picture.png","full_name":"ammaam mamamama","joined_date":"2018-04-21","joined_date_in_string":"21 April 2018","address":"","nickname":""}' WHERE id = 23

Spot the single quote (') in the query in my answer above

As suggested by Barmar in the comments above, its better to use Prepared Statements.

Refer https://www.w3schools.com/php/php_mysql_prepared_statements.asp for help on Prepared statements in PHP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM