如何在php中验证表单字段

时间:2012-09-06 10:56:44

标签: php mysql validation insert email-validation

我有一个注册表单,其中包含一些必填字段。我想检查是否填写了这些必填字段,以及在我插入数据库之前它们是否填写正确。

必填字段之一是电子邮件,我还想检查输入的电子邮件是否是有效的电子邮件。

我的代码如下。

先谢谢你的帮助,我真的很感激。

<?php
include 'config.php';
$tbl_name="citizens"; // Table name
// Get values from form and formatting them as SQL strings
$firstname = mysql_real_escape_string($_POST['firstname']);
$middlename = mysql_real_escape_string($_POST['middlename']);
$lastname = mysql_real_escape_string($_POST['lastname']);
$sex = mysql_real_escape_string($_POST['sex']);
$address = mysql_real_escape_string($_POST['address']);
$employer = mysql_real_escape_string($_POST['employer']);
$posincom = mysql_real_escape_string($_POST['posincom']);
$states = mysql_real_escape_string($_POST['states']);
$agerange = mysql_real_escape_string($_POST['agerange']);
$income = mysql_real_escape_string($_POST['income']);
$email = mysql_real_escape_string($_POST['email']);
$phone = mysql_real_escape_string($_POST['phone']);

// Insert data into mysql 
$sql="INSERT INTO `$tbl_name` (firstname, middlename, lastname, sex, address, employer, position_in_company, states, age_range, local_govt_area, email, phone) VALUES('$firstname', '$middlename', '$lastname', '$sex', '$address', '$employer', '$posincom', '$states', '$agerange', '$income', '$email', '$phone')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "You Have Successful Registered";
}else {
echo "Sorry!!! Could Not Register You. All a* fields must be field.";
}
?>

5 个答案:

答案 0 :(得分:2)

<?php
include 'config.php';
$tbl_name="citizens"; // Table name

$required = array('email');
$errors = array();

foreach($required as $required_fieldname){
    if(!isset($_POST[$required_fieldname]) || empty($_POST[$required_fieldname])){
        $errors[] = 'Sorry!!! Could Not Register You. All a* fields must be field.';
        break;
    }
}

if(isset($_POST['email']) && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
    $errors[] = "That is not a valid email address.";
}

if(count($errors) == 0){
    // Get values from form and formatting them as SQL strings
    $firstname = mysql_real_escape_string($_POST['firstname']);
    $middlename = mysql_real_escape_string($_POST['middlename']);
    $lastname = mysql_real_escape_string($_POST['lastname']);
    $sex = mysql_real_escape_string($_POST['sex']);
    $address = mysql_real_escape_string($_POST['address']);
    $employer = mysql_real_escape_string($_POST['employer']);
    $posincom = mysql_real_escape_string($_POST['posincom']);
    $states = mysql_real_escape_string($_POST['states']);
    $agerange = mysql_real_escape_string($_POST['agerange']);
    $income = mysql_real_escape_string($_POST['income']);
    $email = mysql_real_escape_string($_POST['email']);
    $phone = mysql_real_escape_string($_POST['phone']);

    // Insert data into mysql 
    $sql="INSERT INTO `$tbl_name` (firstname, middlename, lastname, sex, address, employer, position_in_company, states, age_range, local_govt_area, email, phone) VALUES('$firstname', '$middlename', '$lastname', '$sex', '$address', '$employer', '$posincom', '$states', '$agerange', '$income', '$email', '$phone')";
    $result= mysql_query($sql);

    // if successfully insert data into database, displays message "Successful". 
    if($result){
        echo "You Have Successfully Registered";
    }else {
        echo "A technical error has occured.";
    }
}
else{
    echo '<strong>ERRORS!</strong><br>';
    foreach($errors as $error){
        echo $error . '<br>';
    }
}
?>

答案 1 :(得分:0)

您应该在使用JavaScript在客户端提交之前验证表单,如果未正确填写则提醒用户。验证后,允许它提交。 在其他情况下,在服务器上验证是开销,而不是在客户端再次向用户发送响应。

答案 2 :(得分:0)

对于电子邮件,您可以使用https://stackoverflow.com/questions/3314493/check-for-valid-email-address中的此(或类似)功能来验证电子邮件

function isValidEmail($email){
    return preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", $email);
}

或者

function isValidEmail( $email ){
    return filter_var( $email, FILTER_VALIDATE_EMAIL );
}

其余的,您可以使用以下

<?php
    $error = '';

    //put chosen function here
    function isValidEmail( $email ){
        return filter_var( $email, FILTER_VALIDATE_EMAIL );
    }

    //get values and validate each one as required
    $firstname = mysql_real_escape_string($_POST['firstname']);
        if(!$firstname){ $error .= "First name is required<br />"; }

    //repeat for each field
    $email = mysql_real_escape_string($_POST['email']);
        if(!isValidEmail($email)){ $error .= "The email entered is invalid<br />"; }

    //and so on...

    if(!$error){
         //add insert into database code here
    }
    else{
        //display $error however you want e.g....
        echo "<div class=\"error\">$error</div>";
    }
?>

答案 3 :(得分:0)

<?php
include 'config.php';
$tbl_name="citizens"; // Table name
// Get values from form and formatting them as SQL strings

//your other fields ...
$email = mysql_real_escape_string($_POST['email']);
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ 
$errors = 1;
echo "Please enter a correct email address";
}

//similar approach can be used for other fields..
// this is one of the simplest validating approach


if($errors == 0){
// Insert data into mysql 
$sql="INSERT INTO `$tbl_name` (firstname, middlename, lastname, sex, address, employer, position_in_company, states, age_range, local_govt_area, email, phone) VALUES('$firstname', '$middlename', '$lastname', '$sex', '$address', '$employer', '$posincom', '$states', '$agerange', '$income', '$email', '$phone')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "You Have Successful Registered";
}else {
echo "Sorry!!! Could Not Register You. All a* fields must be field.";
}
}
?>

答案 4 :(得分:0)

1。)您可以使用PHP_FILTER进行验证。

2。)你可以在插入数据之前正确检查(变量是否为null)如果变量为null则显示错误msg otherwish insert ..