0

我遇到了变量问题。似乎我必须始终在使用 $variable 之前对其进行初始化。这很烦人。$username_check=mysql_num_rows($sql_username_check); 这里它说注意:未定义的变量:c://中的用户名检查注意:未定义的变量:c://中的电子邮件检查我正在检查数据库中的重复数据。不幸的是,它正在添加相同的数据:(有人可以帮助我吗?

// Database duplicate Fields Check
        $sql_username_check = mysql_query("SELECT user_id FROM users WHERE user_username='$username' LIMIT 1");
        $sql_email_check = mysql_query("SELECT user_id FROM users WHERE user_email='$email' LIMIT 1");
        if($sql_username_check != "")
            if(isset($username_check))
                $username_check= mysql_num_rows($sql_username_check);
        if($sql_email_check != "")
            if(isset($email_check))
                $email_check = mysql_num_rows($sql_email_check); 
        if ($username_check > 0){ 
            $errorMsg = "<u>ERROR:</u><br />Your User Name is already in use inside of our system. Please try another.";
        } 
        else if ($email_check > 0){ 
            $errorMsg = "<u>ERROR:</u><br />Your Email address is already in use inside our system. Please try another.";
        } 
4

4 回答 4

0

你有一些非常奇怪的代码,但你可以试试这个:

    $sql_username_check = mysql_query("SELECT user_id FROM users WHERE user_username='$username' LIMIT 1");
    $sql_email_check = mysql_query("SELECT user_id FROM users WHERE user_email='$email' LIMIT 1");

    if($sql_username_check)
      $username_check= mysql_num_rows($sql_username_check);

    if($sql_email_check)
      $email_check = mysql_num_rows($sql_email_check); 

    if (isset($username_check) && $username_check > 0){ 
        $errorMsg = "<u>ERROR:</u><br />Your User Name is already in use inside of our system. Please try another.";
    } else if (isset($email_check) && $email_check > 0){ 
        $errorMsg = "<u>ERROR:</u><br />Your Email address is already in use inside our system. Please try another.";
    } 
于 2013-02-18T19:43:39.913 回答
0

问题似乎从这里开始:

$sql_username_check = mysql_query("SELECT user_id FROM users WHERE user_username='$username' LIMIT 1");
$sql_email_check = mysql_query("SELECT user_id FROM users WHERE user_email='$email' LIMIT 1");

函数调用不返回字符串,因此您在下面的比较并没有按照您的想法进行。导致错误的行可能是这些:

if ($username_check > 0){ 
        $errorMsg = "<u>ERROR:</u><br />Your User Name is already in use inside of our system. Please try another.";
    } 
    else if ($email_check > 0){ 
        $errorMsg = "<u>ERROR:</u><br />Your Email address is already in use inside our system. Please try another.";
    }

从我从您的示例中读到的内容,我想不出任何方式可以在这里设置$username_check$email_check设置,这样您就不会收到这些通知)。

您真正需要做的是了解如何处理来自数据库的结果集,包括数据库错误处理。

在您阅读这些内容时,您应该继续并转储您对mysql_*函数的使用,因为它们已被弃用并学习使用mysqliPDO哪些是 PHP 中最流行的首选数据库库。

于 2013-02-18T19:46:47.960 回答
0
if(isset($username_check))

这是在$username_check分配之前使用的,这就是为什么会有通知。

试试这样的代码:

$sql_username_check = mysql_query("SELECT user_id FROM users WHERE user_username='$username' LIMIT 1");
        $sql_email_check = mysql_query("SELECT user_id FROM users WHERE user_email='$email' LIMIT 1");
        if($sql_username_check != "")
            $username_check= mysql_num_rows($sql_username_check);
        if($sql_email_check != "")
            $email_check = mysql_num_rows($sql_email_check); 
        if ($username_check > 0){ 
            $errorMsg = "<u>ERROR:</u><br />Your User Name is already in use inside of our system. Please try another.";
        } 
        else if ($email_check > 0){ 
            $errorMsg = "<u>ERROR:</u><br />Your Email address is already in use inside our system. Please try another.";
        } 
于 2013-02-18T19:48:21.367 回答
-1

您需要在 PHP 中调整错误报告设置。例如像这样:

error_reporting (E_ALL & ~E_NOTICE & ~E_STRICT);
于 2013-02-18T19:41:39.313 回答