简体   繁体   中英

Cant veryfi data from mysql database

I have a table ver_code in my database and inside the table there is only one row called code and i have inserted few verification code like ABCDEF , GHIJKL for instance now my following code has failed to verify code from my table using a simple form below

<?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>

i doubt this line

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

it should be

$row['code'] == $ver_code; as $ver_code is simple post variable not an array.

EDIT: if you need to verify from all rows

$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";}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM