PHP MySQL表单验证

时间:2016-08-03 00:52:48

标签: php mysql

我目前正在使用php和mysql进行项目。我是PHP的新手,我希望能够在表单操作完成之前检查表单是否有错误。 (表单操作将信息发送到mySQL数据库)。

if(isset($_POST['SendOrder'])) {  

$FirstName1Err = $LastName1Err = $Email1Err = $Phone1Err = $productsErr = $Size1Err = $Quantity1Err = $Price1Err = ""; 

if (empty($_POST['FirstName1'])) { 
$FirstName1Err = "Your first name is required"; } 

if (empty($_POST['LastName1'])) { 
$LastName1Err = "Your last name is required"; } 

if (empty($_POST['Email1'])) { 
$Email1Err = "Your email is required"; } 

if (empty($_POST['Phone1'])) { 
$Phone1Err = "Your phone number is required"; }  

if (empty($_POST['products'])) { 
$productsErr = "Choosing a product is required"; } 

if (empty($_POST['Size1'])) { 
$Size1Err = "Choosing a size is required"; } 

if (empty($_POST['Quantity1'])) { 
$Quantity1Err = "Choosing a quantity is required"; } 

if (empty($_POST['Price1'])) { 
$Price1Err = "Choosing a price is required"; }

}  

session_start(); 
require 'NFTconnect2.php';  

$epr=''; 
$msg=''; 

if(isset($_GET['epr'])) 
$epr=$_GET['epr']; 



if($epr=='save')
    { 
        $FirstName1 = $_POST['FirstName1']; 
        $LastName1 = $_POST['LastName1'];  
        $Email1 = $_POST['Email1'];  
        $Phone1 = $_POST['Phone1'];  
        $products = $_POST['products'];  
        $Size1 = $_POST['Size1'];   
        $Quantity1 = $_POST['Quantity1'];   
        $Price1 = $_POST['Price1'];

 $a_sql=mysqli_query($con, "INSERT INTO Inventory VALUES('','$FirstName1','$LastName1', '$Email1', '$Phone1', '$products', '$Size1', '$Quantity1', '$Price1')"); 

        if($a_sql)
            $msg='Your order was successful!'; 
        else 
            $msg= 'Your order was not successful! '; 

    }  

基本上我的问题是:我如何编写一个“if”语句,以便我可以先检查表单的验证,然后如果验证通过,那么我可以运行代码的第二部分。提前致谢

3 个答案:

答案 0 :(得分:0)

在开头添加一个名为$hasError = false;

的变量

对于每行添加错误,请设置$hasError = true;,如下所示:

if (empty($_POST['FirstName1'])) { 
$FirstName1Err = "Your first name is required"; 
$hasError = true;} 

if (empty($_POST['LastName1'])) { 
$LastName1Err = "Your last name is required"; 
$hasError = true;} 

//ETC

然后,将代码的第二部分包含在if中,如下所示:

if (!$hasError) {  //If hasError is still false
  //Do stuff
}

我确实有必要提一下,有更有效的方法来验证每个领域,虽然这不是你关心的问题,也不是你的问题,所以我会留下我的答案:)

答案 1 :(得分:0)

不要检查一堆input语句中的所有if是否为空,而是将input的所有名称放入数组中:

$required = array('input1', 'input2', 'input3'); // change input1, input2... to what you require

然后,使用foreach循环检查并查看是否有任何输入为空(位于$required数组内部:

foreach($_POST as $key=>$value){
    if(empty($value) && in_array($key, $required){
        $errors = true; // set errors to true
        echo $key . 'Is required.' // output error
    }
}

现在,要检查验证是否已过,只需检查是否未设置$errors

if(!$errors){
    // validation passed
}

显然,您可以根据自己的需要定制此代码。但是,如果您需要任何帮助,只需发表评论,我会尽力帮您解决问题:)

希望这有助于节省您的时间!

答案 2 :(得分:-2)

我建议使用Javascript进行验证,而不是在后端进行验证。

示例:


function validateForm(){
    var validateFname = document.forms [" myForm"] [" FirstName1"]。value;
    if(validateFname == null || validateFname ==""){
        警告("名称必须填写");
        返回false;
    }
}