PHP表单/准备好的语句错误

时间:2019-02-22 00:19:20

标签: php mysql mysqli prepared-statement

我正在创建用户注册表格,但准备好的声明有问题。如果我在代码中添加exit();,则注册将不会导致任何地方。如果我不添加exit();,则该表单将显示正确的错误消息,但是在第二次尝试后将其关闭。

还有另一个(新手)问题,如何为已注册的电子邮件实施错误消息?我添加了$sql = "SELECT * FROM users WHERE user_name=? AND user_email=?";mysqli_stmt_bind_param($stmt, "ss", $name, $email);,但无法显示错误消息。

到目前为止,对于准备好的报表问题:

else {
        $sql = "SELECT * FROM users WHERE user_name=?";
        $stmt = mysqli_stmt_init($con);
        if (!mysqli_stmt_prepare($stmt, $sql)) {
            header("Location: ../index.php?sqlerror");
            exit();
        }

        else {
        mysqli_stmt_bind_param($stmt, "s", $name);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_store_result($stmt);
        $resultCheck = mysqli_stmt_num_rows($stmt);
        if ($resultCheck > 0) {
        array_push($error_array, "Oops! This username is already taken.<br>");
        exit();
        }

1 个答案:

答案 0 :(得分:2)

您不需要出口,因为您已经有了标题 对于问题注册电子邮件:  您可以使用函数num_rows http://php.net/manual/pt_BR/mysqli-result.num-rows.php

 $mysqli = new mysqli("localhost","root", "", "tables");
 $query = $mysqli->prepare("SELECT * FROM users WHERE user_name=? AND 
  user_email=?");
 $query->execute();
 $query->store_result();

 $rows = $query->num_rows;

并假设是否

if($rows>0){
 //message error
}