无法将我的记录插入数据库?

时间:2013-09-09 19:09:06

标签: php mysql

这是我的注册表单,在我添加验证后,我的记录无法插入到数据库中。但是我的验证工作正常。如果我交换了验证代码和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
        }
    }   

}       
?>

对于我可怜的英语,我很抱歉。

2 个答案:

答案 0 :(得分:0)

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

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

我怀疑您要将该行更改为if (empty($errors) === false),以便只有在 错误时才输入代码的那一部分。

答案 1 :(得分:0)

对于像$test这样的变量的使用应该非常小心,如果是字符串则应该是'.$test.'。您还应该使用mysqli