2014-06-21 64 views
-2

未捕獲異常'PDOException'錯誤我得到了這種錯誤,請有誰能幫我修復它。如果電子郵件重複或不在數據庫中,則在放置代碼以進行過濾之後,會出現錯誤。運行我的代碼後,我的pdo

這裏是我得到的錯誤:

img

<?php 
$host = "localhost"; 
$user = "root"; 
$pass = ""; 
$db = "test"; 

$dbc = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass); 
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 


$name = @$_POST['name']; 
$age = @$_POST['age']; 
$address = @$_POST['address']; 
$gender = @$_POST['gender']; 
$email = @$_POST['email']; 

     $dupesql = "SELECT * FROM students WHERE email = :email "; 

     $dupesql = $dbc->prepare($dupesql); 
     $dupesql->bindParam(':name', $email); 
     $dupesql->execute(); 
     $num_rows = $dupesql->rowCount(); 

     if($num_rows === 0) 
     { 
     echo "1"; 
     $q = "INSERT INTO students(name, age, address, gender, email) VALUES(:name, :age, :address, :gender, :email)"; 

     $query = $dbc->prepare($q); 
     $query->bindParam(':name', $name); 
     $query->bindParam(':age', $age); 
     $query->bindParam(':address', $address); 
     $query->bindParam(':gender', $gender); 
     $query->bindParam(':email', $email); 

     $results = $query->execute(); 


     }else{ 
     echo "0"; 
     exit; 
     }  

?> 
+2

'$ dupesql-> bindParam(':name',$ email);'?它應該':電子郵件' –

+0

@ 1nflktd哦,謝謝我錯過了...把它放在回答,所以我可以投票 – user3762241

+1

爲什麼你沒有發現異常?爲什麼在$ _POST前面有沉默操作符?你爲什麼使用bindParam而不是bindValue?你知道兩者的區別嗎? –

回答

1

那麼你是因爲你在你的查詢中使用了錯誤的參數面臨這樣的錯誤。

$dupesql->bindParam(':name', $email); 

:name不存在,那麼它應該:email