[英]Issue with a php prepared statement throwing a null error
我所遇到的問題似乎與我創建准備好的sql語句以插入一些數據的方式有關。 我最初的想法是某些東西為空,但是我知道數據在那里,因為我已經用回聲測試了它。 我的想法是必須與語句的編寫方式有關,但我無法弄清楚它可能出什么問題。
碼:
// Set up the sql command
$stmt = $mysqli->prepare("INSERT INTO tblcomments (imgpath, author, comment) VALUES(?, ?, ?)");
// Bind the data
$stmt->bind_param("imgpath", $imagePath);
$stmt->bind_param("author", $author);
$stmt->bind_param("comment", $comment);
// Execute the sql
$stmt->execute();
// Close the statement
$stmt->close();
問題是插入語句本身,至少根據錯誤是這樣:
Fatal error: Uncaught Error: Call to a member function prepare() on null in D:\Inetpub\vhosts\kreativecoating.com\httpdocs\commentDataPostHandler.php:55 Stack trace: #0 {main} thrown in D:\Inetpub\vhosts\kreativecoating.com\httpdocs\commentDataPostHandler.php on line 55
第55行是帶有insert語句的行。 所以我的問題是我對准備好的語句做錯了什么嗎? 我已經檢查了數據庫字段中的輸入錯誤,所以不是那樣。 sql中沒有列出一個id字段,但是多數民眾贊成在鍵和自動增量,所以它不需要在那里
綁定參數是全部。
bind_param("ssi", $string, $string, $number);
S代表字符串
我為整數
d表示浮點數/十進制數
我做了一個查詢生成器,實際上生成了所有代碼。 只需插入列名稱即可。 它有助於准備查詢:)
如果您的數據庫連接正常,則可以在bindParam()語句上嘗試此操作。 由於您使用的是問號,因此插入值應如下所示:
$ stmt-> bindParam(1,$ image);
$ stmt-> bindParam(2,$ author);
$ stmt-> bindParam(3,$ comment);
$ stmt-> execute();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.