Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有bind的PHP select语句_Php - Fatal编程技术网

带有bind的PHP select语句

带有bind的PHP select语句,php,Php,我试图通过PHP验证一个注册表单,其中包含准备好的语句和绑定参数,如下所示: $email = $_POST['email']; $password = $_POST['password']; $selectStatement = "SELECT id FROM users WHERE email = ? "; $stmts = mysqli_prepare($dbc, $selectStatement); mysqli_stmt_bind_param($stmts,'s', $email);

我试图通过PHP验证一个注册表单,其中包含准备好的语句和绑定参数,如下所示:

$email = $_POST['email'];
$password = $_POST['password']; 
$selectStatement = "SELECT id FROM users WHERE email = ? ";
$stmts = mysqli_prepare($dbc, $selectStatement);
mysqli_stmt_bind_param($stmts,'s', $email);
mysqli_stmt_execute($stmts);
$count = mysqli_stmt_num_rows($stmts);

if (!$stmts){
    die('mysqli error: '.mysqli_error($dbc));
}
while($row = mysqli_stmt_fetch($stmts)){
    if($row != null){   
        echo('User already registered!');
    }
    }else{  
        $insertQuery = "INSERT INTO users(email, password) VALUES(?, ?)";
        $stmtI = mysqli_prepare($dbc, $insertQuery); 
        mysqli_stmt_bind_param($stmtI, "ss", $email, $password);
        mysqli_stmt_execute($stmtI);
        echo('Registration completed!');
}
}
mysqli_stmt_close($stmts);  
mysqli_stmt_close($stmtI);
mysqli_close($dbc); 

但是当在数据库中找不到用户时,它应该在else分支上运行,但显然它没有到达那里,甚至在while循环中也没有。我做错了什么

你的代码错了。。。在

}
}else{
只写

}else{
试试这个

$email = $_POST['email'];
$password = $_POST['password'];
$selectStatement = "SELECT id FROM users WHERE email = ? ";
$stmts = mysqli_prepare($dbc, $selectStatement);
mysqli_stmt_bind_param($stmts,'s', $email);
mysqli_stmt_execute($stmts);
$count = mysqli_stmt_num_rows($stmts);

if (!$stmts){
    die('mysqli error: '.mysqli_error($dbc));
 }
while($row = mysqli_stmt_fetch($stmts)){
if($count == 1){
    echo('User already registered!');
}else{
    $insertQuery = "INSERT INTO users(email, password) VALUES(?, ?)";
    $stmtI = mysqli_prepare($dbc, $insertQuery);
    mysqli_stmt_bind_param($stmtI, "ss", $email, $password);
    mysqli_stmt_execute($stmtI);
    echo('Registration completed!');
    }
}
mysqli_stmt_close($stmts);
mysqli_stmt_close($stmtI);
mysqli_close($dbc);

While循环仅在从数据库(mysqli_stmt_fetch($stmts))中找到记录时执行。

您必须像这样更改代码

$email = $_POST['email']; 
$password = $_POST['password']; $selectStatement = "SELECT id FROM users WHERE email = ? "; 
$stmts = mysqli_prepare($dbc, $selectStatement);
mysqli_stmt_bind_param($stmts,'s', $email);
mysqli_stmt_execute($stmts);
$count = mysqli_stmt_num_rows($stmts);
if (!$stmts){ die('mysqli error: '.mysqli_error($dbc));
}
    if($count){
            echo('User already registered!');
    }else{
            $insertQuery = "INSERT INTO users(email, password) VALUES(?, ?)";
            $stmtI = mysqli_prepare($dbc, $insertQuery); 
            mysqli_stmt_bind_param($stmtI, "ss", $email, $password);
            mysqli_stmt_execute($stmtI);
            echo('Registration completed!');
    }

}
的捕捉很好,我没有注意到!请注意,我将if($row!=null){to if($count==1){替换为用户不共享电子邮件地址,这是理所当然的。在大多数系统中,无论如何都不应该出现这种情况,但我看到了异常。哦,是的{也是,但这不是问题所在,我以前也尝试过使用计数,但出于某种原因,它总是返回0,即使用户在数据库中。如果没有任何结果,则
while
甚至不会启动,因此无法转到else,它甚至无法到达if!这使得
if
多余同样,如果您在
中输入
,而
中有一个
$row
,即!=null。请不要将密码存储为纯文本。是的,就是这样!我以前也尝试过使用count方法,但出于某种原因,它总是返回0,即使用户在数据库中。请在mysqli_stmt_execute/*store result*/mysqli_stmt_sto之后添加此代码re_result($stmt);是的,store_result函数丢失了所有内容!谢谢!