-1

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

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

我的代码如下。

提前感谢您的帮助,我真的很感激。

<?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.";
}
?>
4

5 回答 5

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>';
    }
}
?>
于 2012-09-06T11:05:13.707 回答
0

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

于 2012-09-06T11:04:55.697 回答
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>";
    }
?>
于 2012-09-06T11:06:26.290 回答
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.";
}
}
?>
于 2012-09-06T11:08:41.537 回答
0

1.)您可以PHP_FILTER用于验证。

2.)您可以在插入数据之前正确检查(变量是否为空)如果变量为空,则显示错误消息,否则插入..

于 2012-09-06T11:15:25.697 回答