doudan5136 2015-11-07 15:02
浏览 39
已采纳

PHP SQL语法错误(没有线索为什么)[重复]

This question already has an answer here:

I am trying to execute an INSERT INTO query but for some reason an error is thrown.

$result1 = mysqli_query($connection, 
             "INSERT INTO `results` 
                     (`result_quiz_name`, `result_marks`, `result_grade`, 
                      `student_id`, `result_max_marks`) 
              VALUES ('Ionic Bonding Introduction', $marks, $grade, 
                $student, 5)");

The variable $marks is an integer, the $grade variable is a string and the $student variable is just the integer value of a session variable. The error that is displayed is this:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 1, 5)' at line 1

I just can't seem to work out where the syntax error is. I have run this query in phpMyAdmin and it works fine (obviously substituting the variables for values).

Thanks,

</div>
  • 写回答

5条回答 默认 最新

  • dongxian1921 2015-11-07 15:06
    关注

    When a column type is text/string/char/varchar etc you have to wrap the variable in quotes in your parameter list, just like you did for 'Ionic Bonding Introduction'

    $result1 = mysqli_query($connection, 
             "INSERT INTO `results` 
                     (`result_quiz_name`, `result_marks`, `result_grade`, 
                      `student_id`, `result_max_marks`) 
              VALUES ('Ionic Bonding Introduction', $marks, '$grade', 
                $student, 5)");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 速帮,学校需要在外上班没空
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义