0

我的代码中有这 3 个错误。我该如何修复它们?

警告:mysqli_query() 期望参数 1 为 mysqli,整数在 D:\hosting....\html\template_signupfunctions.php 第 14 行给出

警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,在第 15 行给出的 inD:\hosting....\html\template_signupfunctions.php 为空

警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,布尔值在 D:\hosting....\html\template_signupfunctions.php 第 48 行给出

警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,布尔值在 D:\hosting....\html\template_signupfunctions.php 第 52 行给出

这是我的代码。

    session_start();
// If user is logged in, header them away
if(isset($_SESSION["username"])){
    header("location: message.php?msg=NO to that weenis");
    exit();
}

// Ajax calls this NAME CHECK code to execute
if(isset($_POST["usernamecheck"])){
    $db_conx = include_once("php_includes/db_connect.php");
    $username = preg_replace('#[^a-z0-9]#i', '', $_POST['usernamecheck']);
    $sql = "SELECT id FROM users WHERE username='$username' LIMIT 1";
    $query = mysqli_query($db_conx, $sql); 
    $uname_check = mysqli_num_rows($query) ;
    if (strlen($username) < 3 || strlen($username) > 16) {
        echo '<strong style="color:#F00;">3 - 16 characters please</strong>';
        exit();
    }
    if (is_numeric($username[0])) {
        echo '<strong style="color:#F00;">Usernames must begin with a letter</strong>';
        exit();
    }
   if ($uname_check < 1) {
        echo '<strong style="color:#009900;">' . $username . ' is OK</strong>';
        exit();
    } else {
        echo '<strong style="color:#F00;">' . $username . ' is taken</strong>';
        exit();
    }
}

// Ajax calls this REGISTRATION code to execute
if(isset($_POST["u"])){
    // CONNECT TO THE DATABASE
    $db_conx = include_once("php_includes/db_connect.php");
    // GATHER THE POSTED DATA INTO LOCAL VARIABLES
    $u = preg_replace('#[^a-z0-9]#i', '', $_POST['u']);
    $e = mysqli_real_escape_string($db_conx, $_POST['e']);
    $p = $_POST['p'];
    $g = preg_replace('#[^a-z]#', '', $_POST['g']);
    $c = preg_replace('#[^a-z ]#i', '', $_POST['c']);
    // GET USER IP ADDRESS
    $ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
    // DUPLICATE DATA CHECKS FOR USERNAME AND EMAIL
    $sql = "SELECT id FROM users WHERE username='$u' LIMIT 1";
    $query = mysqli_query($db_conx, $sql); 
    $u_check = mysqli_num_rows($query);
    // -------------------------------------------
    $sql = "SELECT id FROM users WHERE email='$e' LIMIT 1";
    $query = mysqli_query($db_conx, $sql); 
    $e_check = mysqli_num_rows($query);
    // FORM DATA ERROR HANDLING
    if($u == "" || $e == "" || $p == "" || $g == "" || $c == ""){
        echo "The form submission is missing values.";
        exit();
    } else if ($u_check > 0){ 
        echo "The username you entered is alreay taken";
        exit();
    } else if ($e_check > 0){ 
        echo "That email address is already in use in the system";
        exit();
    } else if (strlen($u) < 3 || strlen($u) > 16) {
        echo "Username must be between 3 and 16 characters";
        exit(); 
    } else if (is_numeric($u[0])) {
        echo 'Username cannot begin with a number';
        exit();
    } else {
    // END FORM DATA ERROR HANDLING
        // Begin Insertion of data into the database
        // Hash the password and apply your own mysterious unique salt
        $cryptpass = crypt($p);
        include_once ("php_includes/randStrGen.php");
        $p_hash = randStrGen(20)."$cryptpass".randStrGen(20);
        // Add user info into the database table for the main site table
        $sql = "INSERT INTO users (username, email, password, gender, country, ip,      signup, lastlogin, notescheck)       
                VALUES('$u','$e','$p_hash','$g','$c','$ip',now(),now(),now())";
        $query = mysqli_query($db_conx, $sql); 
        $uid = mysqli_insert_id($db_conx);
        // Establish their row in the useroptions table
        $sql = "INSERT INTO useroptions (id, username, background) VALUES ('$uid','$u','original')";
        $query = mysqli_query($db_conx, $sql);
        // Create directory(folder) to hold each user's files(pics, MP3s, etc.)
        if (!file_exists("user/$u")) {
            mkdir("user/$u", 0755);
        }
        // Email the user their activation link
        $to = "$e";                          
        $from = "auto_responder@dragonsdengamming.com";
        $subject = 'Dragons Den Gaming Account Activation';
        $message = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Dragons Den Gaming</title></head><body style="margin:0px; font-family:Tahoma, Geneva, sans-serif;"><div style="padding:10px; background:#333; font-size:24px; color:#CCC;"><a href="http://www.yoursitename.com"><img src="images/logo.png" width="200" height="200" alt="yoursitename" style="border:none; float:left;"></a>yoursitename Account Activation</div><div style="padding:24px; font-size:17px;">Hello '.$u.',<br /><br />Click the link below to activate your account when ready:<br /><br /><a href="http://www.yoursitename.com/activation.php?id='.$uid.'&u='.$u.'&e='.$e.'&p='.$p_hash.'">Click here to activate your account now</a><br /><br />Login after successful activation using your:<br />* E-mail Address: <b>'.$e.'</b></div></body></html>';
        $headers = "From: $from\n";
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\n";
        mail($to, $subject, $message, $headers);
        echo "signup_success";
        exit();
    }
    exit();
}
4

2 回答 2

0

好吧,看来这个问题碰巧比我一开始想的更难解决。

include 无法按照您使用它的方式工作。
它不返回任何内容,只是从包含的文件中运行代码。
所以,很可能你必须以这种方式运行它

include_once("php_includes/db_connect.php");

如果您已$db_conx在此文件中设置 - 一切都会正常。

但还有其他问题

  1. 您没有使用准备好的语句
  2. 您正在使用 mysqli,而必须使用 PDO
于 2013-03-13T07:18:40.160 回答
-1

也许您应该查看有关如何设置数据库连接的手册:http: //php.net/manual/de/mysqli.query.php

php_includes/db_connect.php 的来源是什么?

于 2013-03-13T07:08:33.047 回答