我的PDO代码总是将0返回给ajax

时间:2014-06-20 18:12:15

标签: php html mysql sql pdo

我想做的是检查数据库中是否已存在用户名,如果没有,它将执行插入查询。

我的问题是它总是回显0,即使名称不在数据库中。

<?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'];
$imageName = @$_FILES['image']['name'];


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

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

    if($dupe->rowCount() == 0) 
    {
        echo "0";
        exit;   
    }
    else
    {
         echo "1";

        $q = "INSERT INTO students(name, age, address, gender, imageName ) VALUES(:name, :age, :address, :gender, :image)";

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

        $results = $query->execute();
    }
?>

0 个答案:

没有答案