我的pdo中未捕获异常'PDOException'错误

时间:2014-06-21 16:23:29

标签: php mysql sql database 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;
        }       

?>

1 个答案:

答案 0 :(得分:1)

您正面临此错误,因为您在查询中使用了错误的参数。

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

:name不存在,因此应:email