ISSET Php mysql

时间:2013-08-24 03:57:27

标签: php mysql

伙计们,每当我点击我的表单上的提交按钮而没有文本框中的任何数据时,就会说“回复”“你的用户名或电子邮件已被删除!”; 。尽管形式没有任何数据,它怎么还能通过呢?什么是最好的解释?

if(isset($_POST['username']) && isset($_POST['email']))
{
    $username = $_POST['username'];
    $email = $_POST['email'];

    $extract= mysql_query("SELECT * FROM users where username='$username'");

    $resultq = mysql_num_rows($extract);
    if($resultq > 0)
    {
       echo 'Either your username, or email is already taken!';
       return;
    }

}

请你坚持这条线12个小时,我似乎无法继续前进:(

3 个答案:

答案 0 :(得分:2)

isset对空字符串呈现true。它检查变量的存在。这是一个detailed explanation

您应该使用if (trim($_POST['username']) != "")

答案 1 :(得分:0)

if(isset($_POST['submit']))
{
    $username = $_POST['username'];
    $email = $_POST['email'];

    $extract= mysql_query("SELECT * FROM users where username='$username'");

    $resultq = mysql_num_rows($extract);
    if($resultq > 0)
    {
       echo 'Either your username, or email is already taken!';
       return;
    }

}

答案 2 :(得分:0)

您还可以尝试!empty

如果您想同时检查usernameemail already exist,还需要在email查询中查看select

    if(!empty($_POST['username']) && !empty($_POST['email']))
    {
        $username = $_POST['username'];
        $email = $_POST['email'];

        $extract= mysql_query("SELECT * FROM users where username='$username' OR email='$email'");

        $resultq = mysql_num_rows($extract);
        if($resultq > 0)
        {
           echo 'Either your username, or email is already taken!';
           return false;
        }
        else
        {
            echo 'Accept';
            return true;
        }
    }