0

这是我的注册表单,在我添加验证后,我的记录无法插入数据库。但我的验证正在工作。如果我交换验证代码和 mysql,即使验证出错,也可以插入数据库。

<?php
$fnameErr=$lnameErr=$passErr=$repassErr=$icErr=$emailErr=$add1Err=$add2Err=$postErr=$mobileErr="";
$fname=$lname=$pass=$repass=$ic=$email=$add1=$add2=$postcode=$mobile="";

if (isset($_POST['submitbtn']))
{
    $fname    = $_POST['bname'];
    $lname    = $_POST['lname'];
    $pass     = $_POST['bpass'];
    $repass   = $_POST['bconpass'];
    $ic       = $_POST['bic'];
    $email    = $_POST['bemail'];
    $add1     = $_POST['badd1'];
    $add2     = $_POST['badd2'];
    $postcode = $_POST['bpostcode'];
    $mobile   = $_POST['bmobile'];
    $country  = $_POST['bcountry'];
    $state    = $_POST['bstate'];
    $city     = $_POST['bcity'];
    $gen      = $_POST['bgender'];

        if($fname==""||$lname==""||$pass==""||$repass==""||$ic==""||$email==""||$add1==""||$add2==""||$country==""||$state==""||$postcode==""||$mobile==""||$city==""||$gen=="")                 
    {
    ?>
        <script type="text/javascript">
            alert("Please fill in all the required informations.");
        </script>
    <?php
    }


 if (empty($errors) === true)
    {

            //bemail
            if (filter_var($_POST['bemail'], FILTER_VALIDATE_EMAIL) === false) 
            {
                $emailErr = 'A valid email address is required';

            }
            else if (email_exists($_POST['bemail']) === true) 
            {
                $emailErr= 'Sorry, the email \'' . $_POST['bemail'] . '\' is already in use';

            }

            //fname xx
            if (!preg_match("/^[A-Z][a-zA-Z -]+$/i",$_POST['bname'])) 
            {
                $fnameErr= 'Your first name cannot contain with any symbol and number';

            }

            //lname xx
            if (!preg_match("/^[A-Z][a-zA-Z -]+$/i",$_POST['lname']) )
            {
                $lnameErr= 'Your last name cannot contain with any symbol and number';

            }
            //ic xx
            if(!preg_match("/^\d{6}-\d{2}-\d{4}$/i", $_POST['bic'])) 
            {
                $icErr= 'Your ic cannot contain any character / must insert "-"';

            }
            //mobile xx
            if (!preg_match("/^\d{3}-\d{7}$/i", $_POST['bmobile'])) 
            {
                $mobileErr= 'Phone must comply with this mask: 010-1111111 or 0111-1111111';

            }
            //password
            if (strlen($pass) < 6) 
            {
                $passErr = 'Your password must be at least 6 characters';

            }

            //re-password
            if ($_POST['bpass'] !== $_POST['bconpass']) 
            {
                $repassErr= 'Your password do not match';

            }

            //add1 xx
            if (!preg_match("/^[a-zA-Z0-9 _.,:\"\']+$/i", $_POST['badd1'])) 
            {
                $add1Err = 'Address 1 must be only letters, numbers or one of the following';

            }

            //add2 xx
            if (!preg_match("/^[a-zA-Z0-9 _.,:\"\']+$/i",$_POST['badd2'])) 
            {
                $add2Err= 'Address 2 must be only letters, numbers or one of the following';

            }

            //postcode xx
            if (!preg_match("/^\d{5}$/i", $_POST['bpostcode'])) 
            {
                $postErr = 'Postcode must be 5 digits';

            }   

    ?>
        <script type="text/javascript">
        alert("Register have some error,please complete your informations.");
        </script>
    <?php
    }
    else
    {

        $result = mysql_query("select * from member where Member_Email='$email'");                                                 
        if (mysql_num_rows($result)==0)
        {
            mysql_query("insert into member(Member_Name,Member_Lname,Member_Pass,Member_IC,Member_Email,Member_Street1,Member_Street2,Member_Country,Member_State,Member_Postcode,Member_HP,Member_City,Member_Gen) VALUES ('$fname','$lname','$pass','$ic','$email','$add1','$add2','$country','$state','$postcode','$mobile','$city','$gen')");
            header("Location:all_login.php");           

        ?>
            <script type="text/javascript">
            alert('Registered successfully!');
            </script>
        <?php
        }

        else
        {
?>
            <script type="text/javascript">
                alert("email address already exists!");                                                                           
            </script>
<?php
        }
    }   

}       
?>

我很抱歉我的英语很差。

4

2 回答 2

0

if (empty($errors) === true)

在此之前,您发布的代码中没有定义$errors. 根据该函数的 PHP 文档,如果变量不存在,则empty()返回 true。因此,除非您遗漏了某些内容,否则您的代码将始终输入if语句的第一部分而不是第二部分。

我怀疑您想将该行更改为,以便仅在出现错误if (empty($errors) === false)输入该部分代码。

于 2013-09-09T19:20:00.213 回答
0

你应该非常小心使用变量,比如$testif '.$test.'字符串。你也应该使用mysqli

于 2013-09-09T19:27:22.440 回答