简体   繁体   English

为什么会出现所有这些错误?

[英]Why am I getting all these errors?

Sorry if this seems pretty dumb but simple but I can't seem to figure out why I am getting these errors: 抱歉,这看起来很愚蠢但很简单,但是我似乎无法弄清楚为什么会出现这些错误:

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\\Program Files (x86)\\EasyPHP-5.3.9\\www\\Image Upload\\func\\user.func.php on line 25 警告:mysql_result()期望参数1为资源,在第25行的C:\\ Program Files(x86)\\ EasyPHP-5.3.9 \\ www \\ Image Upload \\ func \\ user.func.php中给出布尔值

Notice: Undefined index: user_id in C:\\Program Files (x86)\\EasyPHP-5.3.9\\www\\Image Upload\\register.php on line 37 注意:第37行上的C:\\ Program Files(x86)\\ EasyPHP-5.3.9 \\ www \\ Image Upload \\ register.php中未定义的索引:user_id

Here is the code for each file 这是每个文件的代码

user.func.php: user.func.php:

function user_exists($email){
$email = mysql_real_escape_string($email);
$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `email` = `$email`");
return (mysql_result($query, 0) == 1) ? true : false;
}

and here is register.php: 这是register.php:

if (isset($_POST['register_email'], $_POST['register_name'], $_POST['register_password'])){
$register_email = $_POST['register_email'];
$register_name = $_POST['register_name'];
$register_password = $_POST['register_password'];

$errors = array();

if(empty($register_email) || empty($register_name) || empty($register_password)){
    $errors[] = 'All fields must be filled out';
} 
else{
        if(filter_var($register_email, FILTER_VALIDATE_EMAIL) === false){
            $errors[] = 'Email address not valid';
        }
        if(strlen($register_email) > 255 || strlen($register_name) > 35 || strlen($register_password) > 35){
            $errors[] = 'One or more fields contains too many characters';
        }
        if(user_exists($register_email) === true){
            $errors[] = 'That email has already been registered to another user';
        }
    }

if(!empty($errors)){
    foreach ($errors as $error){
        echo $error, '<br />';
    }
} else {
    $register = user_register($register_email, $register_name, $register_password);
    $SESSION['user_id'] = $register;
    echo $_SESSION['user_id'];
}

}

Thanks for any help! 谢谢你的帮助! -TechGuy24 -TechGuy24

The query is failing .. it should be email = '$email' (instead of surrounding the second email with backticks). 查询失败..应为email = '$email' (而不是第二封电子邮件带有反引号)。

Please also look up prepared statements and PDO. 还请查找准备好的语句和PDO。

mysql_query will return FALSE (a boolean) when it fails and the "resource" you are seeking when it succeeds. mysql_query失败时将返回FALSE (布尔值),成功时将返回您正在寻找的“资源”。

您在值上使用反引号“`”,因此查询失败,请使用单引号'

$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$email'");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM