2012-09-11 97 views
0

我必須將值插入我的表中的PDO塊如下PDO聲明未能執行

try{ 

     $user = 'root'; 
     $pass = null; 
     $pdo = new PDO('mysql:host=localhost; dbname=divebay', $user, $pass); 

     $name = $_POST['name']; 
     $desc = $_POST['description']; 
     $cond = $_POST['condGroup']; 
     $sprice = $_POST['sprice']; 
     $iprice = $_POST['iprice']; 
     $incprice = $_POST['incprice']; 
     $duration = $_POST['duration']; 
     $img = $_POST['img']; 


     $owner = $_SESSION['username']; 

     $valid = "set"; 
     $stmt2 = $pdo->prepare("SELECT * FROM auction WHERE ID = :id"); 
     $stmt2->bindParam(":id", $random, PDO::PARAM_INT); 
     while(isset($valid)){ 
      $random = rand(100000,999999); 
      $stmt2->execute(); 

      if(!$stmt2->fetch(PDO::FETCH_ASSOC)){ 
       unset($valid); 
      } 
     } 

     $timestamp = time() + ($duration * 24 * 60 * 60); 

     $stmt = $pdo->prepare("INSERT INTO auction(ID, name, owner, holder, sprice, iprice, incprice, etime, img, condition, description) 
         VALUES (:id, :name, :owner, :holder, :sprice, :iprice, :incprice:, :etime, :img, :condition, :description"); 
     $stmt->bindParam(':id', $random, PDO::PARAM_INT); 
     $stmt->bindParam(':name', $name, PDO::PARAM_STR); 
     $stmt->bindParam(':owner', $owner, PDO::PARAM_STR); 
     $stmt->bindParam(':holder', $owner, PDO::PARAM_STR); 
     $stmt->bindParam(':iprice', $iprice, PDO::PARAM_STR); 
     $stmt->bindParam(':sprice', $sprice, PDO::PARAM_STR); 
     $stmt->bindParam(':incprice', $incprice, PDO::PARAM_STR); 
     $stmt->bindParam(':etime', $timestamp, PDO::PARAM_INT); 
     $stmt->bindParam(':img', $img, PDO::PARAM_STR); 
     $stmt->bindParam(':condition', $condition, PDO::PARAM_STR); 
     $stmt->bindParam(':description', $description, PDO::PARAM_STR); 
     if($stmt->execute()){ 
      $worked ="yes"; 
     } 

}catch(PDOException $e){ 
     echo $e->getMessage(); 
} 

我不能告訴爲什麼這條語句不會執行,在$工作變量尚未設置時,它是腳本運行。所有的數據庫列名和數據類型都按照原樣檢查了正確的。我從來沒有對一個到目前爲止還沒執行的聲明有任何問題。怎麼了?我怎麼去調試呢?

+1

什麼'的var_dump($語句)'輸出:你一個在語句的末尾缺少)? – andrewsi

+0

你忘了在第二個'準備'中關閉'''' – Mageek

+0

@Bundy只需要添加')'就行了嗎? – Mageek

回答

4

如果您使用錯誤模式異常來設置數據庫連接,那麼如果語句出現問題,PDO將拋出異常。我也看到你正在使用PDO的MySQL驅動程序。如果你這樣做,你應該總是禁用模擬的準備好的語句。因此,作爲以下我會寫你的連接(注意,我還設置了編碼):

$pdo = new PDO('mysql:host=localhost; dbname=divebay;charset=utf8', $user, $pass); 
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

另見this post有關它的更多信息。

一旦你完成了這一步,你會看到你的陳述是錯誤的。

$stmt = $pdo->prepare("INSERT INTO auction(ID, name, owner, holder, sprice, iprice, incprice, etime, img, condition, description) 
         VALUES (:id, :name, :owner, :holder, :sprice, :iprice, :incprice:, :etime, :img, :condition, :description)"); 
                                   ^
1

修改這一行:

$stmt = $pdo->prepare("INSERT INTO auction(ID, name, owner, holder, sprice, iprice, incprice, etime, img, condition, description) 
         VALUES (:id, :name, :owner, :holder, :sprice, :iprice, :incprice:, :etime, :img, :condition, :description"); 

$stmt = $pdo->prepare("INSERT INTO auction(ID, name, owner, holder, sprice, iprice, incprice, etime, img, condition, description) 
         VALUES (:id, :name, :owner, :holder, :sprice, :iprice, :incprice:, :etime, :img, :condition, :description)"); 

的差是)末。

並告訴我它現在是否適用。