空($ _ POST ['性别'])&&和$ _POST ['性别'] ==""

时间:2015-02-19 09:14:41

标签: php

<?php
    $con = mysql_connect("localhost","root","") or die("Your not connected".mysql_error());
    mysql_select_db("detail") or die("Select Database First");
        $error = false;
    //    $password = md5($_POST['password']);
    if(isset($_POST['submit']))
    {
        if (empty($_POST['student_name']))
        {
            $error = true;
            $stud_error = "You can't leave this empty. ";
        }
        if (empty($_POST['user_name']) && $_POST['user_name'] == "")
        {
            $error = true;
            $user_error = "You can't leave this empty. ";
        } 
        if (empty($_POST['password']) && $_POST['password'] == "")
        {
            $error = true;
            $passError = "You can't leave this empty. ";
        }
        if (empty($_POST['confirm_password']) && $_POST['confirm_password'] == "")
        {
            $error = true;
            $confirmError = "Confirm your password here. ";
        }
        if (empty($_POST['gender']) && $_POST['gender'] == "")
        {
             $error = true;
             $genderError = "You can't leave this empty.";
        }
        if (empty($_POST['Year']) && $_POST['Year'] == "")
        {
            if (empty($_POST['Month']) && $_POST['Month'] == "")
            {
                if(empty($_POST['Day']) && $_POST['Day'] == "")
                {
                        $error =true;
                        $birthError = "You can't leave this empty.";
                }     
            }
        }   
        if (empty($_POST['email']) && $_POST['email'] == "")
        {
            $error =true;
            $emailError="You can't leave this empty.";            
        }           
        if ($error == false)
        {
           $password = md5($_POST['password']);

            $date = $_POST['Year'].'-'.$_POST['Month'].'-'.$_POST['Day']; 
            $sex = $_POST['gender'];
            $abc="INSERT INTO student (stud_name,username,stud_pass,sex,birthdate,email)VALUES('$_POST[student_name]','$_POST[user_name]','$password','$sex','$date','$_POST[email]')";
            mysql_query($abc);   
        }
    }
?>
<body>
dkjkjdk
</body>

3 个答案:

答案 0 :(得分:4)

if (empty($_POST['user_name']) && $_POST['user_name'] == ""))

这是一个愚蠢的条件,杀了第二个。阅读手册的内容

<强> empty

  

如果var存在并且具有非空的非零值,则返回FALSE。否则返回TRUE。

     

以下内容被认为是空的:

     

•&#34;&#34; (空字符串)

     

•0(0为整数)

     

•0.0(0作为浮点数)

     

•&#34; 0&#34; (0作为字符串)

     

•NULL

     

•FALSE

     

•array()(空数组)

     

•$变种; (声明的变量,但没有值)

这种情况在最好的情况下是多余的。它就像是说一块玻璃杯是空的,如果它没有水:) :)如果它是空的,它就不会有水,为什么要检查?)

答案 1 :(得分:0)

在PHP中,变量为空,如果

  • 空字符串:&#34;&#34; (例如:由$var = "";生成)
  • 0(int)
  • 0.0(浮动)
  • &#34; 0&#34; (零作为字符串,哈哈)
  • NULL
  • FALSE(布尔值)
  • 一个空数组(例如,由$var = array();生成)
  • 没有定义值的变量(例如,由$var;生成)

所以要小心。

参考:http://php.net/manual/en/function.empty.php

答案 2 :(得分:0)

empty()是php函数,它返回boolean值(如果var存在则返回FALSE并且具有非空的非零值。否则返回TRUE。)所以当你调用时 空($ _ POST [&#39; student_name&#39;])表示如果学生姓名字段返回空值,则空函数将返回true,基本上检查您的帖子是否正在获取数据或为空