PHP验证不工作插入查询?

时间:2013-12-12 03:43:23

标签: php mysql

验证是dine.now我很困惑,在这个代码中写入插入查询的位置

<td><input type="text" name="t4"></td>
        <td>
        <div class="ans">
        <?php if(isset($_GET['ans1'])){
            $txt1=$_GET['ans1'];
            echo $txt1;
        }?>
        <?php if(isset($_GET['ans4'])){
            $txt4=$_GET['ans4'];
            echo $txt4;
        }?>
        </div>
        </td>

signup.php

<?php
    include "db.php";
    $user=$_POST['t1'];
    $pass=md5($_POST['t2']);
    $confirm=md5($_POST['t3']);
    $email=$_POST['t4'];
    if($pass == $confirm && !preg_match("/^[a-zA-Z ]*$/",$email)){
    mysql_query("insert into registor(username,password,email) values('$user','$pass','$email')")or die(mysql_error());
    $query="select* from registor where username like '" . 
        mysql_real_escape_string($user) . "'";
    $res =mysql_query($query);
    if(mysql_num_rows($res) > 0) {
      $txt3="username already taken";
      header("location:signup-index.php?ans3=$txt3");
    }
    $query2="select* from registor where email like '" . 
        mysql_real_escape_string($email) . "'";
    $res2 =mysql_query($query2);
    if(mysql_num_rows($res2) > 0) {
      $txt4="email already taken";
      header("location:signup-index.php?ans4=$txt4");
     }
    }
    else{
        $txt2="Password not match";
        $txt1 = "Please enter valid email"; 
        header("location:signup-index.php?ans1=$txt1&ans2=$txt2");
     }
      ?>

在这个代码中添加插入mysql查询的位置。有更好的方法来制作表单。 已插入记录,但它显示已在index.html上采用的电子邮件

1 个答案:

答案 0 :(得分:1)

根据您在评论中提到的评论和问题。我在这里找到了几点。

1)您的两个选择查询都是错误的

 select* from registor .....

应该是

 select * from registor.....
       ^

2)当您的所有验证都正确时,您可以编写插入查询。你已经在循环开始时编写了插入查询。它总是在验证之前插入记录。

<强> signup.php

<?php

include "db.php";
$user = $_POST['t1'];
$pass = md5($_POST['t2']);
$confirm = md5($_POST['t3']);
$email = $_POST['t4'];
if ($pass == $confirm && !preg_match("/^[a-zA-Z ]*$/", $email)) {
    $errorFlag = 0;
    $query = "select * from registor where username like '" .
            mysql_real_escape_string($user) . "'";
    $res = mysql_query($query);
    if (mysql_num_rows($res) > 0) {
        $errorMsg = "username already taken";
        $errorFlag = 1;
    }
    $query2 = "select * from registor where email like '" .
            mysql_real_escape_string($email) . "'";
    $res2 = mysql_query($query2);
    if (mysql_num_rows($res2) > 0) {
        $errorMsg = "email already taken";
        $errorFlag = 1;
    }

    if ($errorFlag) {
        header("location:signup-index.php?msg=$errorMsg");
    } else {
        mysql_query("insert into registor (username,password,email) values('$user','$pass','$email')") or die(mysql_error());
    }
} else {
    $txt2 = "Password not match";
    $txt1 = "Please enter valid email";
    header("location:signup-index.php?ans1=$txt1&ans2=$txt2");
}

显示错误信息如下: -

<td>
    <div class="ans">
        <?php
        if (isset($_GET['msg'])) {
            echo $_GET['msg'];
        }
        ?>
    </div>
</td>