3种不同的代码错误

时间:2013-03-13 07:04:43

标签: php windows mysqli

我的代码中出现了这三个错误。我该如何修理它们?

  

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

     

警告:mysqli_num_rows()期望参数1为mysqli_result,   在线上给出了D:\ hosting .... \ html \ template_signupfunctions.php   15

     

警告: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();
}

2 个答案:

答案 0 :(得分:0)

好吧,似乎问题恰好比起初想的更难解决。

包含不按您使用它的方式工作 除了从包含的文件中运行代码之外,它不会返回任何内容 所以,很可能你必须以这种方式运行它

include_once("php_includes/db_connect.php");

如果您在此文件中设置了$db_conx,那么一切都会有效。

但还有其他问题

  1. 您没有使用预备陈述
  2. 您正在使用mysqli,而必须使用PDO

答案 1 :(得分:-1)

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

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