php中嵌入的sql語句長度問題

這個問題花費了我長達18小時,從昨天中午開始到現在,折騰的我茶飯不思。

問題是這樣的,php編寫的API接收到來自前端的N個數據,然後通過sql插入到數據庫中,我本來是這樣寫的。

$stmt = $this->conn->prepare("INSERT INTO mocaresult(userID, upload,  checkdate, matchtest, copydraw, clockoutline, clocknum, clockpoint, name1, name2, 
name3, reciteinorder, recitereverse, knock1, minus7first, minus7second, minus7third, minus7forth, minus7fifth) VALUES(?,?,?,?,?,?,?,?,?,?,?,
?,?,?,?,?,?,?,?)");
$stmt->bind_param("ssssssss", $userID, $upload, $checkdate, $matchtest, $copydraw, $clockoutline, $clocknum, $clockpoint, $name1, $name2, $name3,
 $reciteinorder, $recitereverse, $knock1, $minus7first, $minus7second, $minus7third, $minus7forth, $minus7fifth);
$result = $stmt->execute();
$stmt->close();
但是不行,數據庫中並沒有插入進去,我花了5個小時排查前端的問題,沒有任何問題,搞得我懷疑人生了。之後我直接把高中的控制變量法用到極致了, 和與這個功能幾乎一致的一個功能進行對比,發現前端沒有絲毫問題,同樣的前端我把數據寫進另一個api中就沒有任何問題,終於把前端的問題排除了。

過了很久,我把php中的sql字段刪除到8個(不要問我爲什麼是8個,我快瘋了一個一個減少的),發現insert into 表名 (8個字段)values (8個?),這樣是可以插入的,9個就不行。

WTF?EXO me?

我從網上搜了好久,也翻牆出去搜,壓根沒人遇到我這種問題。

我最後換了種寫法。

$stmt = $this->conn->prepare("INSERT INTO mocaresult(userID, upload,  checkdate, matchtest, copydraw, clockoutline, clocknum, clockpoint, 
name1, name2, name3, reciteinorder, recitereverse, knock1, minus7first, minus7second, minus7third, minus7forth, minus7fifth) 
VALUES('".$userID."', '".$upload."', '".$checkdate."', '".$matchtest."', '".$copydraw."', '".$clockoutline."', '".$clocknum."', 
'".$clockpoint."', '".$name1."', '".$name2."', '".$name3."', '".$reciteinorder."', '".$recitereverse."', '".$knock1."', 
'".$minus7first."', '".$minus7second."', '".$minus7third."', '".$minus7forth."', '".$minus7fifth."')");
哦,終於可以了。我至今搞不懂爲什麼用?然後綁定變量這種方式,長度不能超過8,是我電腦的問題嗎?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章