2014-12-04 86 views
1

我試圖爲用戶創建可編輯的配置文件。他們點擊編輯按鈕(form-post)返回帶有可編輯信息的頁面(只有當isset($ _ POST [「edit」])在文本區域,輸入和「完成編輯」按鈕時 當我點擊完成編輯。它需要啓動更新的功能,新的信息到數據庫,但它`不更新其返回一個錯誤:在非對象上調用成員函數bind_param()

Call to a member function bind_param() on a non-object 

我的代碼:

if(isset($_POST["cedit"]) && !empty($_POST["fn"]) && !empty($_POST["ln"]) && !empty($_POST["desc"])){ 

    if($stmtq = $mysqli->prepare("UPDATE `sites`.`accounts` SET `fullname` = ? ,`description` = ? WHERE `id` = ? ") && !empty($_POST["fn"]) && !empty($_POST["ln"]) && !empty($_POST["desc"])){ 
     $stmtq->bind_param("ssd", $_POST["fn"]." ".$_POST["ln"], $_POST["desc"], $_SESSION["user_id"]); 
     $stmtq->execute(); 
     $stmtq->close(); 
    } 
} 
+0

在'prepare()'之後執行'print_r($ stmtq);''。還要檢查錯誤日誌並在腳本中添加'error_reporting(E_ALL);'作爲第一件事 – 2014-12-04 18:18:29

+0

Marcin print_r($ stmtq)返回數字1. – 2014-12-04 18:21:54

回答

5

你有操作問題導致$stmtq成爲truefalse布爾型的優先順序,而不是它正在執行的準備好的語句d是。

這是由於鏈條&&條件包裝成相同的條件。他們發生在作業=之前。加一些()。基本上,這相當於:

// $x is always assigned a boolean 
if ($x = (object && true && true)) 

相反的預期

// $x is assigned the object 
if (($x = object) && (true && true)) 

要解決這個問題:

// Wrap the assignment in its own() group to isolate from the && conditions 
if (($stmtq = $mysqli->prepare("UPDATE `sites`.`accounts` SET `fullname` = ? ,`description` = ? WHERE `id` = ? ")) && !empty($_POST["fn"]) && !empty($_POST["ln"]) && !empty($_POST["desc"]) { 
    // Successful prepare() assignment and all other conditions 
    // Proceed with bind_param()/execute() 
} 

雖然增加了幾行,它會更容易閱讀和少容易出現這些優先級問題,先執行prepare()並進行賦值,然後驗證其他條件,反之亦然。

if (!empty($_POST["fn"]) && !empty($_POST["ln"]) && !empty($_POST["desc"])) { 
    if ($stmtq = $mysqli->prepare(.....)) { 
    // All is well, proceed with bind_param()/execute() 
    } 
} 

對於美味的細節,here is PHP's operator precedence chart。邏輯運算符&&的優先級高於=賦值。

+0

不客氣的Michael。 – 2014-12-04 18:25:59

相關問題