我试图在这里动态更新表

$QUERY = "UPDATE `internshala`.`student` SET `High_School` = \'$High_School\', `HS_Percentage` = \'$HS_Percentage\', `Intermediate` = \'$Intermediate\', `I_Percentage` = \'$I_Percentage\', `Graduation` = \'$Graduation\', `G_Score` = \'$G_Score\', `G_Year` = \'$G_Year\', `PG_Year` = \'$PG_Year\', `PostGraduation` = \'$PostGraduation\', `PG_Score` = \'$PG_Score\' WHERE `student`.`id` = '$_SESSION['user_id'];";


引发错误syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

我不能为此找到正确的语法,上面使用的语法是我从phpmyadmin推导出来的。

PS:update语句中使用的所有变量均设置为非null值。

最佳答案

您的查询有很多问题。您在使用双引号定义字符串时转义了单引号。你不需要这个

SET `High_School` = \'$High_School\',


应该读

SET `High_School` = '$High_School',


另外,您在末尾使用会话变量,而不是关闭引号

WHERE `student`.`id` = '$_SESSION['user_id'];";


您需要将数组项目括在大括号中并关闭单引号:

WHERE `student`.`id` = '{$_SESSION['user_id']}';";


您的完整查询应如下所示

$QUERY = "UPDATE `internshala`.`student` SET
  `High_School` = '$High_School',
  `HS_Percentage` = '$HS_Percentage',
  `Intermediate` = '$Intermediate',
  `I_Percentage` = '$I_Percentage',
  `Graduation` = '$Graduation',
  `G_Score` = '$G_Score',
  `G_Year` = '$G_Year',
  `PG_Year` = '$PG_Year',
  `PostGraduation` = '$PostGraduation',
  `PG_Score` = '$PG_Score'
 WHERE `student`.`id` = '{$_SESSION['user_id']}';";


也不要使用此方法。您愿意使用sql注入。您确实应该在准备好的语句中使用PDO或mysqli。看一下这篇文章:How can I prevent SQL injection in PHP?

关于php - 动态更新mysql表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28288390/

10-11 02:53