在准备好的语句中找不到问题的解决方案(过程PHP)

时间:2019-01-03 20:37:15

标签: php session mysqli

<?php

    if(isset($_POST['register-button']))
    {
    include 'dbh.inc.php';
    error_reporting(0);
    $uname = $_POST['username'];
    $name = $_POST['name'];
    $email = $_POST['email'];
    $pwd = $_POST['pass'];
    $rpwd = $_POST['repass'];
    $city = $_POST['city'];
    $age = $_POST['age'];
    $gender = $_POST['gender'];
    $courses = implode(',',$_POST['lang']);

    if(empty($uname) || empty($name) || empty($email) || empty($pwd) ||  empty($rpwd) || empty($city) || empty($age) || empty($gender) || empty($courses)){
        header("Location: ../register.php?error=emptyfields&name=".$name."&email=".$email."&city=".$city."&age=".$age);
        exit();
    }
    elseif(!filter_var($email, FILTER_VALIDATE_EMAIL) && !preg_match("/^[a-zA-Z0-9]*$/", $uname){
        header("Location: ../register.php?error=invaliduname&email");
        exit();
    }
    elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
        header("Location: ../register.php?error=invalidemail&username=".$uname."name=".$name."&city=".$city."&age=".$age);
        exit();
    }
    elseif(!preg_match("/^[a-zA-Z0-9]*$/", $username)){
        header("Location: ../register.php?error=invalidusername&name=".$name."email=".$email."&city=".$city."&age=".$age);
        exit();
    }
    elseif($pwd!=$rpwd){
        header("Location: ../register.php?error=passwordcheck&username=".$uname."name=".$name."&email=".$email."&city=".$city."&age=".$age);
        exit();
    }
    else{
        $sql1 = "select * from users where uname=?";
        $sql2 = "select * from users where email=?";

        $stmt1 = mysqli_stmt_init($conn);
        $stmt2 = mysqli_stmt_init($conn);

        if(!mysqli_stmt_prepare($stmt1,$sql1) && !mysqli_stmt_prepare($stmt2,$sql2)){
            header("Location: ../register.php?error=sqlerror");
            exit();
        }
        else{
            mysqli_stmt_bind_param($stmt1,"s",$uname);
            mysqli_stmt_bind_param($stmt2,"s",$email);

            mysqli_stmt_execute($stmt1);
            mysqli_stmt_execute($stmt2);

            mysqli_stmt_store_result($stmt1);
            mysqli_stmt_store_result($stmt2);

            $resultcheck1=mysqli_stmt_num_rows($stmt1);
            $resultcheck2=mysqli_stmt_num_rows($stmt2);

            if($resultcheck2>0){
                header("Location: ../register.php?error=registeredemail");
                exit();
            }
            elseif($resultcheck1>0){
                header("Location: ../register.php?error=usernametaken");
                exit();
            }
            else{
                $sql="insert into users values(?,?,?,?,?,?,?,?)";
                $stmt=mysqli_stmt_init($conn);
                if(!mysqli_stmt_prepare($stmt,$sql)){
                    header("Location: ../register.php?error=sqlerror");
                    exit();
                }
                else{
                    $hpass = password_hash($pwd, PASSWORD_DEFAULT);
                    mysqli_bind_param($stmt,"ssssssss",$uname,$name,$email,$hpass,$city,$age,$gender,$courses);
                    mysqli_stmt_execute($stmt);

                    session_start();
                    $_SESSION['uid']=$uname;

                    header("Location: ../index.php");
                    exit();
                }
            }
        }
    }
    }
    else
    {
    header("Location: ../index.php");
    exit();
    }
?>
  

警告:mysqli_stmt_bind_param():第50行上C:\ xampp \ htdocs \ php \ sensitive_website \ includes \ signup.inc.php中的无效对象或资源mysqli_stmt

     

警告:mysqli_stmt_execute():第53行的C:\ xampp \ htdocs \ php \ sensitive_website \ includes \ signup.inc.php中的无效对象或资源mysqli_stmt

     

警告:mysqli_stmt_store_result():第56行上C:\ xampp \ htdocs \ php \ sensitive_website \ includes \ signup.inc.php中的无效对象或资源mysqli_stmt

     

警告:mysqli_stmt_num_rows():第59行上C:\ xampp \ htdocs \ php \ sensitive_website \ includes \ signup.inc.php中的无效对象或资源mysqli_stmt

     

致命错误:未捕获错误:调用C:\ xampp \ htdocs \ php \ sensitive_website \ includes \ signup.inc.php:78中未定义的函数mysqli_bind_param()堆栈跟踪:#0 {main}抛出在C:\第78行上的xampp \ htdocs \ php \ response_website \ includes \ signup.inc.php

1 个答案:

答案 0 :(得分:1)

您的问题是,只有在两者的准备工作($stmt1$stmt2中)失败时,您才退出;如果其中一个,您都应该退出他们失败了。更改此行:

if(!mysqli_stmt_prepare($stmt1,$sql1) && !mysqli_stmt_prepare($stmt2,$sql2)){

if(!mysqli_stmt_prepare($stmt1,$sql1) || !mysqli_stmt_prepare($stmt2,$sql2)){

您还应该使用mysqli_error()来找出为什么准备失败,也许将其添加到您重定向到的位置,例如

header("Location: ../register.php?error=sqlerror&msg=" . urlencode(mysqli_error($conn)));

另一个问题是您有错字,第78行的mysqli_bind_param应该是mysqli_stmt_bind_param