1

我的数据库中有一个表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>
4

1 回答 1

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";}
于 2013-05-09T09:33:05.607 回答