将HTML表单中的用户输入与MY SQL数据库中的数据相匹配

时间:2015-12-28 08:12:53

标签: php mysql

我正在尝试在我的网站上设置电话号码验证系统。我正在使用的逻辑是: -

1)用户单击一个按钮,表示已完成电话号码验证。(一个脚本运行,发送带有随机号码的SMS,使用Rand()生成。此号码也存储在数据库中的该用户行中。

  <html>
        <head>
        </head>
           <body>
             <form id="sms2" name="sms2" method="POST" action="index.php?option=com_content&view=article&id=83">
              <table width= "400">

                 <tr>
                   <td align="right" valign="top">Verification Code:</td>
                   <td align="left"><textarea name="veficationcode" cols="82" rows="5" id="veficationcode"></textarea></td>
                 </tr>
                 <tr>
                   <td colspan="4" align="right"><input type="submit" name= "submit" value="submit"/>
                   </td>
                 </tr>
                </table>
              </form>

            </body>
    </html>



<?php


Global $_CB_framework;

$myId = $_CB_framework->myId();
$cbUser =& CBuser::getInstance( $myId );

if ( ! $cbUser ) {
 $cbUser =& CBuser::getInstance( null );
}

$user =& $cbUser->getUserData();

echo$myId;
        $servername = "";
    $username = "";
    $password = "";
    $dbname = "";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $sql = "UPDATE tablename SET phoneverificationcode ='$phverf'
    WHERE id = $myId";


    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();
    ?>

2)用户被重定向到一个页面,在那里他输入他在短信上收到的验证码,并在他这样做后点击提交。脚本运行并从数据库中的该用户行获取数据,并将其与输入的内容进行匹配。如果匹配则获得成功消息,否则他会收到错误消息。

<?php

Global $_CB_framework;

$myId = $_CB_framework->myId();
$cbUser =& CBuser::getInstance( $myId );

if ( ! $cbUser ) {
 $cbUser =& CBuser::getInstance( null );
}

$user =& $cbUser->getUserData();

echo$myId;
    if(isset($_POST['submit'])){
    $verificationcode = $_POST["veficationcode"]; 
    echo "verificationcode".$verificationcode;

    $servername = "";
    $username = "";
    $password = "";
    $dbname = "";

    // Create connection
    $connect = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$connect) {
        die("Connection failed: " . mysqli_connect_error());
    }

    //select the data from the database for the users own row based on his unique id
    $query = "SELECT phoneverificationcode FROM tablename WHERE id=$myId";

    $result = mysqli_query($connect,$query);
    while($row = mysqli_fetch_array($result));
    echo $row[0];

    if($verificationcode == $row){echo "SUCCESS!!!";}

    else{
    echo "verification code mismatch". "Please try again.";

    }
    $connect->close();
    }
    ?>

我面临的问题是它总是说不匹配。不确定我在这里缺少什么?

2 个答案:

答案 0 :(得分:0)

因为您要查看数据库中字段名称phoneverificationcode,请将If-Statement更改为:

  if($verificationcode == $row['phoneverificationcode']){echo "SUCCESS!!!";}

                    else{
                    echo "verification code mismatch". "Please try again.";

                    }

答案 1 :(得分:0)

我把if语句放在while循环中解决了这个问题。 `if($ result = mysqli_query($ connect,$ query)){ while($ row = mysqli_fetch_assoc($ result)){if($ row [“cb_phoneverificationcode”] == $ row [“cb_userinputphonecode”]){echo“SUCCESS !!!”; }

else {echo“验证码不匹配。请再试一次”;} }`