0

我有一个选择语句和一个更新语句。我想在更新语句中将“recipes_saved”的值设置为选择语句的结果。我试过用这个:

$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());

但查询失败。

任何帮助将非常感激。

4

2 回答 2

0

'我认为您的'SELECTFROM 中都有额外的内容,carbohydrates并再次使用 LIMIT ,例如:

尝试复制以下查询:

 $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'] . "'";
于 2013-11-11T14:45:32.580 回答
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'

看看这是否有效。

于 2013-11-11T14:46:04.660 回答