2014-02-07 68 views
0

當我運行我的代碼,我得到以下錯誤:Bind_param()錯誤

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

我現在用Google搜索這個問題的幾個小時,我不能找出問題的所在。我知道這個主題張貼了很多次,但我是PHP初學者,我不明白爲什麼這不起作用。

這是我使用的代碼:

類:

class Item { 
    private $iname; 

    function __construct($name) 
    { 
     $this->iname = $name; 
    } 

    public function addItem() 
    { 
     global $mysqli, $db_table_prefix; 

      $stmt = $mysqli->prepare("INSERT INTO ".$db_table_prefix."items (
        iname, 
        VALUES (
        ? 
        )"); 
      $stmt->bind_param("s", $this->iname); 
      $stmt->execute(); 
      $inserted_id = $mysqli->insert_id; 
      $stmt->close(); 

    } 
} 

和發佈表單頁面:

require_once("models/config.php"); 


if(!empty($_POST)) 
    { 
     $item_name = trim($_POST["iname"]); 

     $item = new Item($item_name); 
     $item->addItem(); 

    } 

require_once("models/header.php"); 
echo " 
<body onload='initialize()'> 
    <div id='wrapper'> 
     <div id='top'><div id='logo'></div></div> 
     <div id='content'> 
      <h>Add new Item</h> 
      <div id='main'> 
       <div id='regbox'> 
        <form name='newItem' action='".$_SERVER['PHP_SELF']."' method='post'> 
         <p> 
         <label>Item description:</label> 
         <input type='text' name='iname' /> 
         </p> 
         <input type='submit' name='Submit' value='Add Item'/> 
        </form> 
       </div> 
      </div> 
     </div> 
    </div> 
</body> 
</html>"; 
?> 
+0

的,什麼是錯誤? –

+0

調用一個非對象的成員函數bind_param() – user3275701

+0

$ mysqli對象在哪裏創建? (mysql連接) –

回答

0

你有一個SQL語法錯誤:

INSERT INTO ".$db_table_prefix."items (
       iname, 
       VALUES (
       ? 
       ) 

這應該是:

INSERT INTO ".$db_table_prefix."items (
       iname) 
       VALUES (
       ? 
       ) 

因爲語法錯誤則不會創建$語句對象,因此誤差Call to a member function bind_param() on a non-object的。

始終輸出MySQL的錯誤信息,如果您有問題:

echo $mysqli->error; 
+0

嗯,我現在覺得很愚蠢,但自從我開始學習PHP之前,我會給自己一個通過:)感謝隊友 – user3275701

相關問題