2014-02-14 91 views
0

我使用ajax,我不能告訴錯誤是什麼,但我確信數據已插入我的數據庫。這裏是我的嘗試:準備聲明插入在PHP失敗在我的情況

if(isset($_POST['user_id']) && isset($_POST['content']) && isset($_POST['date']) && isset($_POST['category_id'])){ 

    $content = $_POST['content']; 
    $date = $_POST['date']; 
    $user_id = $_POST['user_id']; 
    $category_id = $_POST['category_id']; 

    $stmt = $db->prepare("INSERT INTO post_items(`post_id`,`content`,`date`,`user_id`,`category_id`) 
     VALUES (?,?,?,?)"); 
    $stmt = bind_param('ssii',$content,$date,$user_id,$category_id); 

    if($stmt->execute()) { 
     echo mysqli_insert_id($db); 

    }else{ 
     echo "Something is wrong. Insert failed.."; 
    } 
} 

我的舊的工作版本(與缺乏安全的)如下所示:

if(isset($_POST['user_id']) && isset($_POST['content']) && isset($_POST['date']) && isset($_POST['category_id'])){ 

    $content = $_POST['content']; 
    $date = $_POST['date']; 
    $user_id = $_POST['user_id']; 
    $category_id = $_POST['category_id']; 

/* $result = $db->query("INSERT INTO post_items(`content`,`date`,`user_id`,`category_id`) 
     VALUES (".$content."', '".$date."', '".$user_id."', '".$category_id."')");*/ 

    $stmt = $db->prepare("INSERT INTO post_items(`post_id`,`content`,`date`,`user_id`,`category_id`) 
     VALUES (?,?,?,?)"); 
    $stmt = bind_param('ssii',$content,$date,$user_id,$category_id); 

    if($stmt->execute()) { 
     echo mysqli_insert_id($db); 

    }else{ 
     echo "Something is wrong. Insert failed.."; 
    } 
} 

我不知道我做錯了什麼在這裏,因爲它是第一次我已經使用這個材料。

+0

@ RonniSkansing所以它應該如何? – user3277912

+1

一般來說,我會建議檢查錯誤並將它們記錄到ajax代碼中的文件中。然後,您可以在調試時觀看日誌文件,以便您不會完全處於黑暗中。 –

+1

是否有'post_id'設置爲'AUTO_INCREMENT'?如果是這樣,拿出來。另外,你的值/綁定不匹配。 –

回答

0

首先,你不綁定相同數量的值作爲分配參數...

此外,您使用的bind_param的方式是錯誤的。它應該是

if(isset($_POST['user_id']) && isset($_POST['content']) && isset($_POST['date']) && isset($_POST['category_id'])){ 

    $content = $_POST['content']; 
    $date = $_POST['date']; 
    $user_id = $_POST['user_id']; 
    $category_id = $_POST['category_id']; 

    $stmt = $db->prepare("INSERT INTO post_items(`content`,`date`,`user_id`,`category_id`) 
     VALUES (?,?,?,?)"); 
    $stmt->bindParam(1, $content); 
    ... 
    $stmt->bindParam(4, $category_id); 

    if($stmt->execute()) { 
     echo mysqli_insert_id($db); 

    }else{ 
     echo "Something is wrong. Insert failed.."; 
    } 
} 

,或者你也可以做到以下幾點:

if(isset($_POST['user_id']) && isset($_POST['content']) && isset($_POST['date']) && isset($_POST['category_id'])){ 

    $content = $_POST['content']; 
    $date = $_POST['date']; 
    $user_id = $_POST['user_id']; 
    $category_id = $_POST['category_id']; 

    $stmt = $db->prepare("INSERT INTO post_items(`content`,`date`,`user_id`,`category_id`) 
     VALUES (?,?,?,?)"); 

    if($stmt->execute(array($content,$date,$user_id,$category_id))) { 
     echo mysqli_insert_id($db); 

    }else{ 
     echo "Something is wrong. Insert failed.."; 
    } 
} 

也只是爲了讓你知道,PDO有來獲取最後一個ID的方法,所以不是 mysqli_insert_id 你可以使用 $ stmt-> lastInsertId()

if(isset($_POST['user_id']) && isset($_POST['content']) && isset($_POST['date']) && isset($_POST['category_id'])){ 

    $content = $_POST['content']; 
    $date = $_POST['date']; 
    $user_id = $_POST['user_id']; 
    $category_id = $_POST['category_id']; 

    $stmt = $db->prepare("INSERT INTO post_items(`content`,`date`,`user_id`,`category_id`) 
     VALUES (?,?,?,?)"); 
    $stmt->bindParam(1, $content); 
    ... 
    $stmt->bindParam(4, $category_id); 

    if($stmt->execute()) { 
     echo $stmt->lastInsertId(); 

    }else{ 
     echo "Something is wrong. Insert failed.."; 
    } 
} 
+0

沒有工作..什麼是bindParam在做什麼? – user3277912

+0

沒有經過測試就不容易編碼^^,bindParam將值綁定到X「?」它創建(其中x是函數的第一個參數),第二個參數是綁定的值...看看這個鏈接http://www.php.net/manual/en/pdostatement.bindparam.php – Hipny

+0

我複製你的代碼,並做這樣的,但沒有工作$ stmt-> bindParam(1,'ssii'); $ stmt-> bindParam(2,$ content); $ stmt-> bindParam(3,$ date); $ stmt-> bindParam(4,$ user_id); $ stmt-> bindParam(5,$ category_id); – user3277912