php
  • prepared-statement
  • 2016-07-27 69 views -1 likes 
    -1
    //function to add bm 
    function add_bm($new_url, $email) 
    { 
        $db = $this->dbm; 
    
    
        $this->new_url = $new_url; 
        $this->email = $email; 
    
        $sql = "select * from bookmark where email='$this->valid_user' and bm_URL='$this->new_url'"; 
    
        if(!$stmt = $db->conn->query($sql)) 
         { 
    
          echo "query failed: (" . $db->conn->errno . ") " .$db->conn->error; 
         }else{ 
         //echo "can check"; 
         //return true; 
    
    
        //row count 
        if($stmt->num_rows > 0){ 
         echo "<b><br>Sorry ! <br> The URL had already been added . </b> "; 
         return false; 
         }else{ 
        //return true; 
    
        // prepare and bind 
    
         $stmt = $db->conn->prepare("INSERT INTO bookmark (email, bm_URL,) VALUES (?,?)"); 
         $stmt->bind_param("ss", $this->email, $this->new_url); 
    
    
    // set parameters and execute 
        if($stmt->execute()){ 
    
          $stmt->close(); 
         $db->conn->close(); 
    
         return true; 
          } 
         } 
         } 
    
    } 
    
    +0

    看[bind_param(http://php.net/manual/en/pdostatement.bindparam.php) –

    +0

    你在你準備好的聲明中有語法錯誤,因爲你絕對沒有錯誤處理和簡單地假設什麼都不會發生錯誤,你把由失敗的準備和運行返回的布爾錯誤,導致進一步的混亂。 –

    +0

    Typo'bm_URL,'。你應該到處都有約束力。 – chris85

    回答

    0

    當你這樣做DB操作,檢查錯誤,然後再繼續。否則你會得到意想不到的行爲和令人費解的錯誤,如這樣。

    您的$stmt->bind_param不起作用,因爲$stmt===false因爲前面的指令出錯了。

    if(!$stmt = $db->conn->prepare("INSERT INTO ...")){ 
        //something went wrong. This shows the error but handle as appropriate 
        die($db->$conn->error); 
    } 
    //safe to continue 
    $stmt->bind_param(...) 
    
    相關問題