dongquming3255
dongquming3255
2018-03-17 17:34

警告:mysqli_query()期望参数1为mysqli,在第14行的C:\ wamp64 \ www \ Test \ php \ Register.php中给出null [关闭]

已采纳

I'm trying to create a registration system for my site and get the error:

( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp64\www\Test\php\Register.php on line 14

php code:

session_start();
$db = mysqli_connect("localhost", "root", "", "users");
if(isset ($_POST['Register_btm'])) {
    $name = mysqli_real_escape_string($db, $_POST['name']);
    $email = mysqli_real_escape_string($db, $_POST["email"]);
    $username = mysqli_real_escape_string($db, $_POST["username"]);
    $password = mysqli_real_escape_string($db, $_POST["password"]);
    $password2 = mysqli_real_escape_string($db, $_POST["password2"]);

if($password == $password2)
    $password = MD5($password);
    $sql = "INSERT INTO users(id, name, email, username, pssword) VALUES ('null','$name','$email','$username','$pssword')";
    mysqli_query($dm, $sql);
    $_SESSION[message] = "You have successfully registered";
    $_SESSION[username] = $username;
}
else {
    @$_SESSION[message] = " Passwords do not match";
}
?>

What can I do?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • drl37530 drl37530 3年前

    You used $dm instead of $db, here corrected code -

        session_start(); 
        $db = mysqli_connect("localhost", "root", "","users");
        if(isset ($_POST['Register_btm'])) { 
        $name = mysqli_real_escape_string($db, $_POST['name']);
        $email = mysqli_real_escape_string($db, $_POST["email"]);
        $username = mysqli_real_escape_string($db, $_POST["username"]); 
        $password = mysqli_real_escape_string($db, $_POST["password"]); 
        $password2 = mysqli_real_escape_string($db, $_POST["password2"]); 
        if($password == $password2) $password = MD5($password);
    
        $sql = "INSERT INTO users(id, name, email, username, pssword) VALUES 
        ('null','$name','$email','$username','$pssword')"; 
        mysqli_query($db, $sql);
    
        $_SESSION[message] = "You have successfully registered"; 
        $_SESSION[username] = $username; 
        } else { 
        @$_SESSION[message] = " Passwords do not match";
        } 
        ?>
    
    点赞 评论 复制链接分享
  • douluo6626 douluo6626 3年前

    You have typo in mysqli_query($dm, $sql); Change it to mysqli_query($db, $sql);

    点赞 评论 复制链接分享