我有一个select语句和一个update语句。我想在update语句中做的是将'recipes_saved'的值设置为select语句的结果。我试过用这个:
$query = "UPDATE `users` SET `recipes_saved` = ('SELECT `recipe_1_name` FROM `carbohydrates`') WHERE `user_id` = '" . $_SESSION['user_id'] . "'";
$data= mysqli_query($dbc,$query) or die('Query failed: ' . mysqli_error());
但查询失败。
非常感谢任何帮助。
答案 0 :(得分:0)
我认为您的'SELECT 还有 FROM '
中的额外carbohydrates
,并再次使用LIMIT,如:< / p>
尝试复制以下查询:
$query = "UPDATE `users` SET `recipes_saved` = (SELECT `recipe_1_name` FROM `carbohydrates` LIMIT 1) WHERE `user_id` = '" . $_SESSION['user_id'] . "'";
如果你想让它不那么混乱,你当然可以删除后面的勾号,例如:
$query = "UPDATE users SET recipes_saved = (SELECT recipe_1_name FROM carbohydrates LIMIT 1) WHERE user_id = '" . $_SESSION['user_id'] . "'";
答案 1 :(得分:0)
据我所知,您的查询中不需要这么多引号。尝试:
$query = "UPDATE users SET recipes_saved = (SELECT recipe_1_name FROM carbohydrates) WHERE user_id='" . $_SESSION['user_id'] . "'";
直接登录数据库(命令行或GUI客户端)并尝试运行查询也很有用:
UPDATE users SET recipes_saved = (SELECT recipe_1_name FROM carbohydrates) WHERE user_id='username'
看看是否有效。