为什么我会收到所有这些错误?

时间:2012-08-29 00:19:48

标签: php mysql easyphp

很抱歉,如果这看起来很愚蠢但很简单,但我似乎无法弄清楚为什么我会收到这些错误:

警告:mysql_result()要求参数1为资源,布尔值在第25行的C:\ Program Files(x86)\ EasyPHP-5.3.9 \ www \ Image Upload \ func \ user.func.php中给出

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

以下是每个文件的代码

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;
}

这里是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'];
}

}

感谢您的帮助! -TechGuy24

2 个答案:

答案 0 :(得分:1)

查询失败..它应该是email = '$email'(而不是用反引号包围第二封电子邮件)。

请查阅准备好的声明和PDO。

mysql_query将在失败时返回FALSE(布尔值),并在成功时返回“资源”。

答案 1 :(得分:0)

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

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