简体   繁体   English

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

[英]Cannot find solution to the problem in prepared statement(procedural php)

<?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();
    }
?>

Warning: mysqli_stmt_bind_param(): invalid object or resource mysqli_stmt in C:\\xampp\\htdocs\\php\\responsive_website\\includes\\signup.inc.php on line 50警告:mysqli_stmt_bind_param(): 无效的对象或资源 mysqli_stmt in C:\\xampp\\htdocs\\php\\responsive_website\\includes\\signup.inc.php 第 50 行

Warning: mysqli_stmt_execute(): invalid object or resource mysqli_stmt in C:\\xampp\\htdocs\\php\\responsive_website\\includes\\signup.inc.php on line 53警告:mysqli_stmt_execute():第 53 行 C:\\xampp\\htdocs\\php\\responsive_website\\includes\\signup.inc.php 中的无效对象或资源 mysqli_stmt

Warning: mysqli_stmt_store_result(): invalid object or resource mysqli_stmt in C:\\xampp\\htdocs\\php\\responsive_website\\includes\\signup.inc.php on line 56警告:mysqli_stmt_store_result():第 56 行 C:\\xampp\\htdocs\\php\\responsive_website\\includes\\signup.inc.php 中的无效对象或资源 mysqli_stmt

Warning: mysqli_stmt_num_rows(): invalid object or resource mysqli_stmt in C:\\xampp\\htdocs\\php\\responsive_website\\includes\\signup.inc.php on line 59警告:mysqli_stmt_num_rows():C:\\xampp\\htdocs\\php\\responsive_website\\includes\\signup.inc.php 中第 59 行的无效对象或资源 mysqli_stmt

Fatal error: Uncaught Error: Call to undefined function mysqli_bind_param() in C:\\xampp\\htdocs\\php\\responsive_website\\includes\\signup.inc.php:78 Stack trace: #0 {main} thrown in C:\\xampp\\htdocs\\php\\responsive_website\\includes\\signup.inc.php on line 78致命错误:未捕获的错误:调用 C:\\xampp\\htdocs\\php\\responsive_website\\includes\\signup.inc.php:78 中未定义的函数 mysqli_bind_param() 堆栈跟踪:#0 {main} 抛出在 C:\\xampp\\htdocs \\php\\responsive_website\\includes\\signup.inc.php 第 78 行

Your problem is that you are only exiting if both prepares (of $stmt1 and $stmt2 ) fail, where you should exit if either of them fail.您的问题是,您仅准备( $stmt1$stmt2 )都失败$stmt2退出,如果其中任何一个失败,您应该退出。 Change this line:改变这一行:

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

to

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

You should also use mysqli_error() to find out why the prepare failed, perhaps adding that to the location you redirect to eg您还应该使用mysqli_error()来找出为什么失败的准备,也许并称该位置,您重定向到如

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

Your other problem is that you have a typo, mysqli_bind_param should be mysqli_stmt_bind_param on line 78.你的另一个问题是你有一个错字, mysqli_bind_param应该是第 78 行的mysqli_stmt_bind_param

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM