0

I've got a .php registration file that can successfully insert 4 new fields into a user database in my localhost but whenever I add an empty field error check it will stop working. If I don't use an empty field error check all 4 blank values will be added into my user table in localhost. Here's the code in my .php file:

    <div class="contact">
            <div>
                <h4>Registration Form</h4>
            </div>

            <div>
                <table bgcolor="#CCCCCC">
                    <h4>All fields are necessary and are not to be left empty</h4>
                </br></br>
                <font color="white">Name</font><input class="test1" type="text" id="txNm" value="">
                <font color="white">Username</font><input class="test1" type="text" id="txUsn" value="">
                <font color="white">Password</font><input class="test1" type="password" id="txPwd" value="">
                <font color="white">E-Mail</font><input class="test1" type="text" id="txEml" value="">
                <input class="test2" name="Submit" type="submit" value="" id="submit" onClick='submitUser();txNm.value="";txUsn.value="";txPwd.value="";txEml.value="";'/>

            </table>
            </div>

        </div>

the submitUser function will then jump over to a js file:

    function submitUser(){
var name = $('#txNm').val();
var username = $('#txUsn').val(); 
var password = $('#txPwd').val(); 
var email = $('#txEml').val();


var finalData = {
    nm: name,
    usn: username,
    pwd: password,
    eml: email
};



$.post('submitUser.php', finalData, function(resp){
    if(resp == 'success'){
        alert('Thank you for register.');
        getUserList();
    }
});   

} then it will jump again go to a submitUser.php file containing the rest of the code here's the contents:

    $nm = mysql_real_escape_string($_POST["nm"]);
$usn = mysql_real_escape_string($_POST["usn"]);
$pwd = mysql_real_escape_string($_POST["pwd"]);
$eml = mysql_real_escape_string($_POST["eml"]);

$q = "INSERT INTO userdb (name, username, password, email) VALUES('$nm','$usn','$pwd','$eml')";

     if(!mysql_query($q, $con)){
    die('Error: ' . mysql_error());
    echo mysql_error();
}else{
    echo 'success';
}


mysql_close($con);

the thing is, everytime I add an empty field error check nothing will happen, if I remove it then the whole registration process will continue but well...even if the fields are all empty.

My code is:

   `if(empty($_POST['txNm'])) {
die('You need to enter a value for the Name field');
}`

what is wrong with my empty field error check code?

4

2 回答 2

0

似乎是拼写错误:

-----------------'nm'
if(empty($_POST['txNm'])) {
die('You need to enter a value for the Name field');
}

于 2013-06-06T10:23:27.157 回答
0

尝试这个:

if(empty($nm)) {
die('You need to enter a value for the Name field');
}

此外,这些可能需要更改为:

$nm = mysql_real_escape_string($_POST["txNm"]);
$usn = mysql_real_escape_string($_POST["txUsn"]);
$pwd = mysql_real_escape_string($_POST["txPwd"]);
$eml = mysql_real_escape_string($_POST["txEml"]);
于 2013-06-06T10:28:47.233 回答