未捕获的异常'ErrorException',消息'错误的SQL:INSERT INTO

时间:2014-12-12 22:03:23

标签: php mysql

我没有使用我的INSERT INTO for MySQL获得正确的语法。

        $v0="`" . $this->mysqli->real_escape_string($cooke[0]) . "`";
    $v1="`" . $this->mysqli->real_escape_string($cooke[1]) . "`";
    $v2="`" . $this->mysqli->real_escape_string($cooke[2]) . "`";
    $v3="`" . $this->mysqli->real_escape_string($cooke[3]) . "`";
    $v4="`" . $this->mysqli->real_escape_string($cooke[4]) . "`";
    $sql="INSERT INTO index(surname, surChart, immigration, residence) VALUES ($v0,$v1,$v2, $v3)";

    if($this->mysqli->query($sql) === false) {
      trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $this->mysqli->error, E_USER_ERROR);
    } else {
      $last_inserted_id = $this->mysqli->insert_id;
      $affected_rows = $this->mysqli->affected_rows; echo 'af: '. $affected_rows;
    }

错误是:

Fatal error: Uncaught exception 'ErrorException' with message 'Wrong SQL: INSERT INTO index(surname, surChart, immigration, residence) VALUES (`COOK/E`,`COOK/E|CHARDE|FORD| |COOKE|WHITE|DENSLOW|NASH|FOOTE|SMITH||STRONG|FORD||WOOLCOTT|PITKIN|CLARK|COOLEY|GUNN|GRAVES|FIELD|LONGLEY|ELDRIDGE|O.C. AVERY||WESTWOOD|BARNARD|CATLIN||ALLEN||Abner Avery|NIMS|WOOD|WRIGHT|H.L. AVERY||BURDSALL|PUTNEY|COOKE||HAMBLEN?|FOSKETT|CLARK|ELDRIDGE|LONGLEY|O.C. AVERY||LAWRENCE|BATCHELOR|MORSE|TARBOL/|TARBALL|HOUGHTON|LONGLEY|[COOKE^ Contd.]|FIELD|ELDRIDGE|O.C. AVERY`,`Brideport^ Dorset|England;||||Dorchester|Windsor^ CT|Northampton Westfield|New Haven CT Hadley|||Windsor CT|Windsor CT|||Springfield|Sunderland|Sunderland|Hawley|Charlemont|Hawley|Charlemont||Hadley||Hadley|Hadley|Shelburne||Hardwick|Shelburne||Charlemont|Ohio|Ohio|Ohio|Charlemont||Groton^ Salem||Salem|Salem||Charlton|Charlton||Goshen|Hawley|Charlemont|Charlemont||Groton MA||Sudbury|Groton MA|Lexington||Groton MA|Hawley||||Hawley||Charlemont|`, `---||1630 The Mary & John|||Bef 1637 in C:\xampp\htdocs\AveryChart\mySqlCsv.php on line 107

Sql echo:

INSERT INTO ancestorIndex(surname, surChart, immigration, residence) VALUES (`COOK/E`,`COOK/E+CHARDE+FORD+ +COOKE+WHITE+DENSLOW+NASH+FOOTE+SMITH++STRONG+FORD++WOOLCOTT+PITKIN+CLARK+COOLEY+GUNN+GRAVES+FIELD+LONGLEY+ELDRIDGE+O.C. AVERY++WESTWOOD+BARNARD+CATLIN++ALLEN++Abner Avery+NIMS+WOOD+WRIGHT+H.L. AVERY++BURDSALL+PUTNEY+COOKE++HAMBLEN?+FOSKETT+CLARK+ELDRIDGE+LONGLEY+O.C. AVERY++LAWRENCE+BATCHELOR+MORSE+TARBOL/+TARBALL+HOUGHTON+LONGLEY+[COOKE^ Contd.]+FIELD+ELDRIDGE+O.C. AVERY`,`Brideport^ Dorset+England;++++Dorchester+Windsor^ CT+Northampton Westfield+New Haven CT Hadley+++Windsor CT+Windsor CT+++Springfield+Sunderland+Sunderland+Hawley+Charlemont+Hawley+Charlemont++Hadley++Hadley+Hadley+Shelburne++Hardwick+Shelburne++Charlemont+Ohio+Ohio+Ohio+Charlemont++Groton^ Salem++Salem+Salem++Charlton+Charlton++Goshen+Hawley+Charlemont+Charlemont++Groton MA++Sudbury+Groton MA+Lexington++Groton MA+Hawley++++Hawley++Charlemont+`, `---++1630 The Mary & John+++Bef 1637+1638+1678+Aft 1650+1676+1688++++1656+1661++Aft 1687++1680+1709+1736+1783+1805+1847+1895++++1661+1706+1743++1783+By 1813++1815++1858+1885+1913++By 1643+By 1662+By 1678+By 1700+By 1738+By 1766++1791+1812+1847+1895++By 1639+bef 1667+1660+1687+By 1732+By 1720+By 1749+1773++++By 1827+1847+1895`)

2 个答案:

答案 0 :(得分:1)

你可以做三件事。

首先,INDEX是一个令人难以置信的糟糕的桌子名称选择。这是MySQL中的保留字。

第二,你的界限如下:

 $v1="`" . $this->mysqli->real_escape_string($cooke[1]) . "`";

会产生这样的值。

 `someValue`.

您希望文本值不包含在反引号中,而是包含在撇号(单引号)中。使用它。

 $v1="'" . $this->mysqli->real_escape_string($cooke[1]) . "'";

更好的是,使用绑定变量。阅读:http://php.net/manual/en/mysqli-stmt.bind-param.php

第三,您插入表格某些列的字符串值确实非常长。以下是您要插入的值。我认为它们中的一些可能对于您在表中声明的列太长了。

Surname: COOK/E
Surchart: COOK/E+CHARDE+FORD+ +COOKE+WHITE+DENSLOW+NASH+FOOTE+SMITH++STRONG+FORD++WOOLCOTT+PITKIN+CLARK+COOLEY+GUNN+GRAVES+FIELD+LONGLEY+ELDRIDGE+O.C. AVERY++WESTWOOD+BARNARD+CATLIN++ALLEN++Abner Avery+NIMS+WOOD+WRIGHT+H.L. AVERY++BURDSALL+PUTNEY+COOKE++HAMBLEN?+FOSKETT+CLARK+ELDRIDGE+LONGLEY+O.C. AVERY++LAWRENCE+BATCHELOR+MORSE+TARBOL/+TARBALL+HOUGHTON+LONGLEY+[COOKE^ Contd.]+FIELD+ELDRIDGE+O.C. AVERY
Immigration: Brideport^ Dorset+England;++++Dorchester+Windsor^ CT+Northampton Westfield+New Haven CT Hadley+++Windsor CT+Windsor CT+++Springfield+Sunderland+Sunderland+Hawley+Charlemont+Hawley+Charlemont++Hadley++Hadley+Hadley+Shelburne++Hardwick+Shelburne++Charlemont+Ohio+Ohio+Ohio+Charlemont++Groton^ Salem++Salem+Salem++Charlton+Charlton++Goshen+Hawley+Charlemont+Charlemont++Groton MA++Sudbury+Groton MA+Lexington++Groton MA+Hawley++++Hawley++Charlemont+
Residence: ---++1630 The Mary & John+++Bef 1637+1638+1678+Aft 1650+1676+1688++++1656+1661++Aft 1687++1680+1709+1736+1783+1805+1847+1895++++1661+1706+1743++1783+By 1813++1815++1858+1885+1913++By 1643+By 1662+By 1678+By 1700+By 1738+By 1766++1791+1812+1847+1895++By 1639+bef 1667+1660+1687+By 1732+By 1720+By 1749+1773++++By 1827+1847+1895

答案 1 :(得分:1)

这有效:

    public function arrayToMySQL2($line_of_text){
    if ($line_of_text[0]){
        $stmt = mysqli_prepare($this->mysqli, "INSERT INTO ancestorIndex VALUES (?, ?, ?, ?, ?)");
        mysqli_stmt_bind_param($stmt, 'sssss', $line_of_text[0], $line_of_text[1], $line_of_text[2], $line_of_text[3], $line_of_text[4]);
        mysqli_stmt_execute($stmt);
        printf("%d Row inserted.<br>", mysqli_stmt_affected_rows($stmt));
        mysqli_stmt_close($stmt);
    }
}