Solution to the problem that php user submission form cannot be empty: 1. Modify the code and add some judgments; 2. isset is used to judge whether the variable has been initialized; 2. empty can set the value to ["false", "Empty", "0", "NULL", "Uninitialized"] variables are all judged to be TRUE.
php user submission form cannot be empty solution:
You can modify the code and add some judgments:
The code is as follows:
if(empty($_POST['name'])){ echo "俗话说的好,雁过留声人过留名<br />"; } elseif(empty($_POST['comment'])){ echo "矮油,多说几句吧~"; } else{ $sql = "INSERT INTO myblog_comments(blog_id, dateposted, name, comment) VALUES(" . $validentry . ", NOW(), '" . $_POST['name'] . "', '" . $_POST['comment'] . "');"; mysql_query($sql); header("Location: http://". $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']."?id=" . $validentry); }
PHP null value judgment
empty
and isset
are both variables Processing functions, they are all used to determine whether the variable has been configured, but they have certain differences: empty will also detect whether the variable is empty or zero. When a variable value is 0, empty considers the variable to be equal to empty, which is equivalent to not being set.
The code is as follows:
<?php /*比如检测 $id 变量,当 $id=0 时,用empty 和 isset 来检测变量 $id 是否已经配置,两都将返回不同的值—— empty 认为没有配置,isset 能够取得 $id 的值:*/ $id=0; empty($id)?print "It's empty .":print "It's $id ."; //结果:It's empty . print "<br>"; !isset($id)?print "It's empty .":print "It's $id ."; //结果:It's 0 . ?>
Summary In PHP, "NULL
" and "empty
" are two concepts.
isset is mainly used to determine whether the variable has been initialized;
empty can set the value to "false" or "empty" , "0", "NULL", and "uninitialized" variables are all judged to be TRUE;
is_null Only variables with a value of "NULL" are judged to be TRUE;
var == null Judges variables with values of "false", "empty", "0", and "NULL" as TRUE;
var === null Only variables with a value of "NULL" are judged as TRUE;
So when we judge whether a variable is really "NULL", we mostly use is_null to avoid " false", "0" and other values.
Related learning recommendations: PHP programming from entry to proficiency
The above is the detailed content of How to prevent php user submission form from being empty?. For more information, please follow other related articles on the PHP Chinese website!