无法将字符串插入数据库

时间:2018-04-21 15:40:37

标签: javascript php json

我有一个看起来像这样的json:

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

然后,我有一个帖子请求,我将json发送到php页面。

然后我有一个看起来像这样的SQL:

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

所有与连接相关的东西都很好,当我使用gettype($variable)检查变量的类型时,它会显示字符串,但我无法完成查询。

有人能发现这个问题吗?

1 个答案:

答案 0 :(得分:0)

生成的查询应将user_json值作为字符串。查询应该是

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

在上面的回答中找出查询中的单引号(')

正如Barmar在上述评论中所建议的那样,最好使用准备好的陈述。

请参阅https://www.w3schools.com/php/php_mysql_prepared_statements.asp以获取有关PHP中预准备语句的帮助。