2014-02-07 2 views
0

내 코드를 실행하면 나는 다음과 같은 오류 얻을 :Bind_param() 오류

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

내가 지금 몇 시간 동안이 문제에 대해 인터넷 검색을하고 문제가 어디 있는지 알아낼 질수. 이 주제가 여러 번 게시되지만 저는 PHP 초보자이며 이것이 작동하지 않는 이유를 알지 못합니다.

내가 사용하는 코드입니다 :

등급 (class) :

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(); 

    } 
} 

및 게시 양식 페이지 :

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

:

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

그리고 오류가있는 작업은 다음과 같습니다

항상 출력 MySQL의 오류 메시지가 당신은 문제가 있다면? –

+0

개체가 아닌 개체에서 bind_param() 함수를 호출하십시오. – user3275701

+0

$ mysqli 개체는 어디에 생성 되었습니까? (mysql 연결) –

답변

0

당신은 SQL 구문 오류가 다음과 같아야합니다 :

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

구문 오류로 인해 $ stmt 개체가 만들어지지 않으므로 Call to a member function bind_param() on a non-object 오류가 발생합니다.

echo $mysqli->error; 
+0

글쎄, 지금은 바보 같은 기분이 들지만, PHP를 배우기 시작한 이래로 나는 자기 자신에게 패스를 줄 것이다 :) Thanks mate – user3275701