您的 SQL 语法有错误,但似乎无法跟踪错误


You have an error in your SQL syntax, but cant seem to trace error

我使用以下脚本将数据从表单输入到我的数据库中。我已经回应了开头声明的每个值,它们都很好。

    include("connectmysqli.php"); 
    echo '<link rel="stylesheet" href="http://towerroadacademy.co.uk/templates/rt_reflex_j16/css/template.css">';
    if (isset($_GET['questionnaireID'])) {$questionnaireID = $_GET['questionnaireID'];}else {$questionnaireID = '';}
    if (isset($_POST['newquestionnumber'])) {$questionnumber = $_POST['newquestionnumber'];}
    if (isset($_POST['questionID'])) {$questionID = $_POST['questionID'];}else {$questionID = '';}
    if (isset($_POST['question'])) {$question = $_POST['question'];}else {$question = '';}
    if (isset($_POST['lowerlabel'])) {$lowerlabel = $_POST['lowerlabel'];}else {$lowerlabel = '';}
    if (isset($_POST['middlelabel'])) {$middlelabel = $_POST['middlelabel'];}else {$middlelabel = '';}
    if (isset($_POST['upperlabel'])) {$upperlabel = $_POST['upperlabel'];}else {$upperlabel = '';}
    $stmt = $db->prepare("INSERT INTO `QuestionnaireQuestions` (`questionnaireID`, `questionnumber`, `questionID`, `question`, `lowerlabel`, `middlelabel`, `upperlabel`) VALUES ($questionnaireID', '$questionnumber', '$questionID', '$question', '$lowerlabel', '$middlelabel', '$upperlabel') WHERE questionnaireID='$questionnaireID';");
    if (!$stmt) trigger_error($db->error);
    $stmt->execute(); 

不过,我一直收到以下错误,并且似乎无法追踪导致它的原因。

Notice: 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 '', '3', '1947679104', 'questonofngdfngodfngo',     'lower', 'midddle', 'upper') WHER' at line 1 in     /home2/towerroa/public_html/questionnaires/addanotherquestionsubmit.php on line 16 Fatal error: Call     to a member function execute() on a non-object in     /home2/towerroa/public_html/questionnaires/addanotherquestionsubmit.php on line 17

问卷问题表如下所示:

    id  questionnaireID questionnumber  questionID  question    lowerlabel  middlelabel upperlabel

你缺少$questionnaireID上的引号:

INSERT INTO `QuestionnaireQuestions` (`questionnaireID`, `questionnumber`, `questionID`, `question`, `lowerlabel`, `middlelabel`, `upperlabel`) VALUES ('$questionnaireID', '$questionnumber', '$questionID', '$question', '$lowerlabel', '$middlelabel', '$upperlabel') 

同时删除WHERE子句。

UPDATE语句可以使用 WHERE 语句根据条件更新现有数据库记录。当然INSERT SELECT语句可以包含WHEREINSERT语句本身不能。

INSERT不适用于WHERE条件,如果您只想UPDATE行,则可以使用该条件WHERE并替换它

VALUES ($questionnaireID',......

VALUES ('$questionnaireID',

您错过了单引号,并从末尾删除了";"。现在查询将是

$stmt = $db->prepare("INSERT INTO `QuestionnaireQuestions` (`questionnaireID`,
                      `questionnumber`, `questionID`, `question`, `lowerlabel`,
                      `middlelabel`, `upperlabel`) VALUES ('$questionnaireID',
                      '$questionnumber', '$questionID', '$question', '$lowerlabel',
                      '$middlelabel', '$upperlabel')");

但我必须理解您使用的是 PDO 语句而不是mysql_* 已弃用的函数

($questionnaireID'

应该是

('$questionnaireID'

但是你真的应该尝试使用准备好的语句