-1

好的,所以我正在尝试将用户注册表单添加到我的网站。几天来我一直在尝试解决这个问题,但我尝试过的任何方法似乎都不起作用。目前,当我填写注册表并点击提交按钮时,我会收到此警告。

警告:mysql_num_rows() 期望参数 1 是资源,在第 64 行的 /Applications/XAMPP/xamppfiles/htdocs/tutorials/MySite/index.php 中给出 null

我进行了三次检查,以确保我的数据库和行的语法在脚本和 phpmyadmin 中是相同的,所以我真的迷路了,不知道从这里做什么。我总是无法连接到 phpmyadmin 并进行查询/检索信息。

到目前为止,这就是我所拥有的(对不起这么多代码):

    <?php

$reg = $_POST['reg'];
// declaring variables to prevent errors

$fn      = ""; //first name
$ln      = ""; //last name
$un      = ""; //username
$em      = ""; //email
$em2     = ""; //email 2
$pswd    = ""; //sign up date
$u_check = ""; //check if username exists

//registration form
$fn    = strip_tags($_POST['fname']);
$ln    = strip_tags($_POST['lname']);
$un    = strip_tags($_POST['username']);
$em    = strip_tags($_POST['email']);
$em2   = strip_tags($_POST['email2']);
$pswd  = strip_tags($_POST['password']);
$pswd2 = strip_tags($_POST['password2']);
$d     = date("Y-m-d"); // year - month - day

if ($reg) {
    if ($em == $em2) { //this block of code is where I'm having the most trouble at.
        // Check if user already exists
        $u_check = mysql_query("'SELECT' username 'FROM' users 'WHERE' username='$un'");
        // Count the amount of rows where username= $un
        $check   = mysql_num_rows($u_check); //<<<<<<<<<<<<<<<<<<<<<<<<LINE 64 
        if ($check == 0) { //maybe this should be $u_check??\\




            //check all of the fileds have been filled in
            if ($fn && $ln && $un && $em && $em2 && $pswd && $pswd2) {
                //check that passwords match
                if ($pswd == $pswd2) {
                    //check the maximum length of username/first name/last name does not exceed 25 characters.
                    if (strlen($un) > 25 || strlen($fn) > 25 || strlen($ln) > 25) {
                        echo "The maximum limit for username/first name/ last name is 25 characters!";

                    } else { //check to see if password is allowable length
                        if (strlen($pswd) > 30 || strlen($pswd) < 5) {
                            echo "Your password must be between 5 and 30 characthers long!";
                        } else {
                            //encrypts password using md5 before sending to database
                            $pswd  = md5($pswd);
                            $pswd2 = md5($pswd2);
                            $query = mysql_query("INSERT INTO users VALUES (' ', '$un', '$fn', '$ln', '$em', '$pswd', '$d', '0')");
                            die("<h2>Welcome to MySite</h2>Login to your account to get started....");
                        }
                    }
                } else {
                    echo "Your passwords don't match!";

                }
            } else {
                echo "Please fill in all fields";
            }
        }
    }
}

?>

再一次,我几天来一直试图解决这个问题,但仍然无法弄清楚发生了什么。

4

2 回答 2

1

您收到错误是因为您的查询没有返回要计数的对象。它失败了。

您的查询语法无效:

$u_check = mysql_query ("'SELECT' username 'FROM' users 'WHERE' username='$un'");

应该:

$u_check = mysql_query ("SELECT `username` FROM `users` WHERE `username`='$un'");

出于对人类的热爱,请清理您的输入并切换到PDOmysqli,因为 mysql_ 函数已被弃用

这是一个非常基本的 PDO 示例,假设您按照基本教程连接到数据库:

//assumes $db is your PDO connection
$sql = $db->prepare('SELECT `username` FROM `users` WHERE `username`=:username');
$sql->bindParam(':username', $un, PDO::PARAM_STR);
$sql->execute();
于 2013-05-22T02:26:21.860 回答
1

使用 PDO

$sql= "SELECT `username` FROM `users` WHERE `username` = :username"; 
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':username', $un, PDO::PARAM_STR); 
$stmt->execute();
于 2013-05-22T02:36:13.997 回答