isset() function is returning true even when item is not set

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

Here is my code. For some reason, if I submit the form without placing and passwords in, it still creates the database entry. There are some comments scattered throughout the code, but the code is fairly straightforward. Any ideas?

Sign up';  if($_SERVER['REQUEST_METHOD'] != 'POST') {     /*The form hasn't been posted yet, display it       note that the action="" will cause the form to post to the same page it is on */     echo '
Username:
Password:
Password again:
E-mail:
'; } else { /* so, the form has been posted, we'll process the data in three steps: 1. Check the data 2. Let the user refill the wrong fields (if necessary) 3. Save the data */ $errors = array(); /* declare the array for later use */ if(isset($_POST['user_name'])) { //the user name exists if(!ctype_alnum($_POST['user_name'])) { $errors[] = 'The username can only contain letters and digits.'; } if(strlen($_POST['user_name']) > 30) { $errors[] = 'The username cannot be longer than 30 characters.'; } } else { $errors[] = 'The username field must not be empty.'; } if(isset($_POST['user_pass'])) { if($_POST['user_pass'] != $_POST['user_pass_check']) { $errors[] = 'The two passwords did not match.'; } } else { $errors[] = 'The password field cannot be empty.'; } if(!empty($errors)) { echo 'Uh-oh.. a couple of fields are not filled in correctly..'; echo '
    '; foreach($errors as $key => $value) { echo '
  • '.$value.'
  • '; } echo '
'; } else { //the form has been posted without errors, so save it //notice the use of mysql_real_escape_string, keep everything safe. //also notice the sha1 function which hashes the password $sql = "INSERT INTO users(user_name, user_pass, user_email, user_date, user_level) VALUES('" . mysql_real_escape_string($_POST['user_name']) . "', '" . sha1($_POST['user_pass']) . "', '" . mysql_real_escape_string($_POST['user_email']) . "', NOW(), 0)"; $result = mysql_query($sql); if(!$result) { //something went wrong, display the error echo 'Something went wrong while registering. Please try again later.'; //echo mysql_error(); //debugging purposes, uncomment when needed } else { echo 'Successfully registered. You can now sign in and start posting!'; } } } include 'footer.php'; ?>

回答1:

Null and/or empty strings are still set if the variable is declared. Try this:

if(isset($_POST['user_pass']) && $_POST['user_pass'] != "")



回答2:

isset checks that the variable is set - in this case it is set to '' (an empty string). Try using empty() as well.



回答3:

simple solution: if(!empty($_POST['xxx']) == true) almost equals when you use:

if(isset($_POST['xxx']) == true && $_POST['xxx'] != '') 

except: isset() regards 0(string or number) as true, while empty() regards 0 as true.



回答4:

Check this one too

if(isset($_POST['user_pass']) && !empty($_POST['user_pass'])) 

isset() will check that if the POST is set for 'user_pass' & !empty() will check that if the value is empty.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!