0

我已经验证了我的表格。我想弹出一条验证消息“信息有错误,请插入正确的信息”。但出现2个弹出框“信息有错误,请插入正确的信息”。和“谢谢!您的订单已下达!”。我可以知道是什么问题吗?

    $fnameErr=$lnameErr=$add1Err=$add2Err=$postErr=$mobileErr=$creditErr=$pinErr ="";
    $credit=$pin="";

    if($_REQUEST['command']=='update')
    {
    $date     = date('Y-m-d');
    $time     = date("H:i:s");
    $charge   = $_REQUEST['ocharge'];
    $fname    = $_REQUEST['ofname'];
    $lname    = $_REQUEST['olname'];
    $mobile   = $_REQUEST['omobile'];
    $add1     = $_REQUEST['oadd1'];
    $add2     = $_REQUEST['oadd2'];
    $postcode = $_REQUEST['opostcode'];
    $state    = $_REQUEST['ostate'];
    $country  = $_REQUEST['ocountry'];
    $credit   = $_REQUEST['ocredit'];
    $pin      = $_REQUEST['opin'];
    $city     = $_REQUEST['ocity'];

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

        else
        {

        $sql=" INSERT INTO `order` (Order_Date,Order_Time,Delivery_Charge,
                            Delivery_Fname,Delivery_Lname,Delivery_HP,Delivery_Street1,
                            Delivery_Street2,Delivery_Postcode,Delivery_State,
                            Delivery_Country,Credit_No,Pin_No,Delivery_City,Order_Status) 

                            VALUES 

                            ('$date','$time','$charge','$fname','$lname','$mobile',
                            '$add1','$add2','$postcode','$state','$country',
                            '$credit','$pin','$city','Pending')";

        $result=mysql_query($sql);

        if($result === FALSE)
        {
                die("Query Failed!".mysql_error().$result);
        }

        $orderid=mysql_insert_id();   

        if (empty($errors) === true)
        {
        //fname xx
            if (!preg_match("/^[A-Z][a-zA-Z -]+$/i", $_REQUEST['ofname'])) 
            {
                $fnameErr= 'Your first name cannot contain with any symbol and number';
            }
        //lname xx
            if (!preg_match("/^[A-Z][a-zA-Z -]+$/i",$_REQUEST['olname']) )
            {
                $lnameErr= 'Your last name cannot contain with any symbol and number';
            }
        //add1 xx
            if (!preg_match("/^[a-zA-Z0-9 _.,:\"\']+$/i",$_REQUEST['oadd1'])) 
            {
                $add1Err = 'Address 1 must be only letters, numbers or one of the following';   
            }

        //add2 xx
            if (!preg_match("/^[a-zA-Z0-9 _.,:\"\']+$/i",$_REQUEST['oadd2'])) 
            {
                $add2Err= 'Address 2 must be only letters, numbers or one of the following';    
            }
        //postcode xx
            if (!preg_match("/^\d{5}$/i", $_REQUEST['opostcode'])) 
            {
                $postErr = 'Postcode must be 5 digits';

            }   
        //mobile xx
            if (!preg_match("/^\d{3}-\d{7}$/i", $_REQUEST['omobile'])) 
            {
                $mobileErr= 'Phone must comply with this mask: 010-1111111 or 0111-1111111';    
            }
        //credit card xx
            if (!preg_match("/^\d{4}-\d{4}-\d{4}-\d{4}$/i", $_REQUEST['ocredit'])) 
            {
                $creditErr= 'Credit Card must comply with this mask: 0000-1111-2222-3333';

            }
        //pin xx
            if (!preg_match("/^\d{6}$/i", $_REQUEST['opin'])) 
            {
                $pinErr = 'Pin must be 6 digits';   
            }
            ?>
        <script type="text/javascript">
        alert("Information has some error,please insert the proper information.");
        </script>
    <?php
    }
        $max=count($_SESSION['cart']);
        for($i=0;$i<$max;$i++)
        {
            $pid=$_SESSION['cart'][$i]['productid'];
            $q=$_SESSION['cart'][$i]['qty'];
            $price=get_price($pid);

            $insert_query="INSERT INTO order_detail (Order_ID,Product_ID,Order_Quantity,Sub_Total) VALUES ('$orderid','$pid','$q','$price')";   

            if(mysql_query($insert_query))
            {
                echo"<script>alert('Thank You! your order has been placed!')</script>";
            }
        }

        }
    }
?>
4

1 回答 1

0

您的代码似乎结构不正确。您正在检查各个字段是否存在错误,然后无论是否存在任何错误都显示错误消息。之后,无论错误检查发生了什么,您都将继续保存信息并显示其他警报。

无需过多介绍代码的细节,一种结构化的方法是:

$errors = array();
//fname xx
if (!preg_match("/^[A-Z][a-zA-Z -]+$/i", $_REQUEST['ofname'])) 
{
    $fnameErr= 'Your first name cannot contain with any symbol and number';
    array_push($errors, $fnameErr);
}
//lname xx
if (!preg_match("/^[A-Z][a-zA-Z -]+$/i",$_REQUEST['olname']) )
{
    $lnameErr= 'Your last name cannot contain with any symbol and number';
    array_push($errors, $lnameErr);
}
//add1 xx
if (!preg_match("/^[a-zA-Z0-9 _.,:\"\']+$/i",$_REQUEST['oadd1'])) 
{
    $add1Err = 'Address 1 must be only letters, numbers or one of the following';   
    array_push($errors, $add1Err);
}

//add2 xx
if (!preg_match("/^[a-zA-Z0-9 _.,:\"\']+$/i",$_REQUEST['oadd2'])) 
{
    $add2Err= 'Address 2 must be only letters, numbers or one of the following';    
    array_push($errors, $add2Err);
}
//postcode xx
if (!preg_match("/^\d{5}$/i", $_REQUEST['opostcode'])) 
{
    $postErr = 'Postcode must be 5 digits';
    array_push($errors, $postErr);
}   
//mobile xx
if (!preg_match("/^\d{3}-\d{7}$/i", $_REQUEST['omobile'])) 
{
    $mobileErr= 'Phone must comply with this mask: 010-1111111 or 0111-1111111';    
    array_push($errors, $mobileErr);
}
//credit card xx
if (!preg_match("/^\d{4}-\d{4}-\d{4}-\d{4}$/i", $_REQUEST['ocredit'])) 
{
    $creditErr= 'Credit Card must comply with this mask: 0000-1111-2222-3333';
    array_push($errors, $creditErr);
}
//pin xx
if (!preg_match("/^\d{6}$/i", $_REQUEST['opin'])) 
{
    $pinErr = 'Pin must be 6 digits';   
    array_push($errors, $pinErr);
}

if(count($errors) > 0) {
?>

    <script type="text/javascript">
    alert("Information has some error,please insert the proper information.");
    </script>

<?php
}
else {
    $max=count($_SESSION['cart']);
    for($i=0;$i<$max;$i++)
    {
        $pid=$_SESSION['cart'][$i]['productid'];
        $q=$_SESSION['cart'][$i]['qty'];
        $price=get_price($pid);

        $insert_query="INSERT INTO order_detail (Order_ID,Product_ID,Order_Quantity,Sub_Total) VALUES ('$orderid','$pid','$q','$price')";   

        if(mysql_query($insert_query))
        {
            echo"<script>alert('Thank You! your order has been placed!')</script>";
        }
    }
}
?>

最后,请注意mysql_函数系列已被弃用,您应该尽快停止使用它们。至少使用 PDO 代替mysqli_函数。

于 2013-09-05T10:52:40.057 回答