无法验证来自mysql数据库的数据

时间:2013-05-09 09:30:14

标签: php mysql pdo

我的数据库中有一个表ver_code,在表中只有一行名为code,我插入了很少的验证码,例如ABCDEF,GHIJKL,例如现在我的下面代码未能使用下面的简单表格验证我的表格中的代码

<?php

if (isset($_POST['ver_code']))
    {

        $ver_code = $_POST['ver_code'];

        if(!empty($ver_code)){

            try{
                $conn = new PDO("mysql:host=localhost;dbname=pro1", "pro1", "4931//4931");
            }
            catch(PDOException $pe)
                {
                    die('Connection error, because: ' .$pe->getMessage());
                }

                $sql = "SELECT `code` FROM `ver_code`";
                $stmt = $conn->query($sql);

                if(!$stmt)
                {
                    die("Execute query error, because: ". $conn->errorInfo());
                }

                $stmt->setFetchMode(PDO::FETCH_ASSOC);
                $row = $stmt->fetch();

                if($row['code'] == $ver_code['code']){

                    echo "Account Verified ! ";

                }else{
                    echo "Invalid Verification Code !";
                }



        }else{
         echo "Plz enter a verification code ... ";
        }

    }

?>

<form action="index2.php" method="POST" >
    <input type="text" name="ver_code" />
    <input type="submit" value="Verify" />
</form>

1 个答案:

答案 0 :(得分:2)

我怀疑这一行

$row['code'] == $ver_code['code']

应该是

$row['code'] == $ver_code;因为$ver_code是一个简单的post变量,而不是一个数组。

编辑:如果您需要从所有行验证

$stmt = $conn->prepare("SELECT `code` FROM `ver_code` where code= ?"); 
$stmt->bindParam(1,$ver_code);
$stmt->execute();
if($stmt->rowCount()>0){
echo "Account Verified ";
}else{ echo "Invalid Verification Code";}