加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 准备语句SQL

发布时间:2020-12-13 22:53:45 所属栏目:PHP教程 来源:网络整理
导读:stackoverflow.com的问候社区. 我试图学习预备语句,但我收到了错误信息 我无法解决.我明白它是什么错误,但我不知道如何解决它. 错误消息:警告:mysqli_stmt :: bind_result():绑定变量数与第69行中预准备语句中的字段数不匹配 ?phpif(isset($_POST["submit
stackoverflow.com的问候社区.

我试图学习预备语句,但我收到了错误信息
我无法解决.我明白它是什么错误,但我不知道如何解决它.

错误消息:警告:mysqli_stmt :: bind_result():绑定变量数与第69行中预准备语句中的字段数不匹配

<?php
if(isset($_POST["submit"])){
    //Connect
$mysqli = new mysqli("localhost","member","xxxxxx","websecurity");

if($mysqli->connect_errno) {
  die("Connect failed: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error);
}
//Submitt pushed
if(!empty($_POST['user']) && !empty($_POST['pass'])) {
    $user= strip_tags($_POST['user']);
    $pass= strip_tags($_POST['pass']);

$user = $mysqli->real_escape_string($user);
$pass = $mysqli->real_escape_string($pass);


// Prepare
    $sql = "SELECT * FROM login WHERE username = ? AND password = ?";
    $stmt = $mysqli->prepare($sql);

if(!$stmt) {
    die("Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error);
}

//Prepared statements

$bind_result = $stmt->bind_param("ss",$user,$pass);
if(!$bind_result) {
echo "Bind failed: (" . $stmt->errno . ") " . $stmt->error;
    }


//Lets execute this

//Give a value to statements
    $user =0;
    $pass ='';

                $execute_result = $stmt->execute();
                if(!$execute_result) {
                     echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
                }

        // This line->      $stmt->bind_result($user,$pass);



                $success = mysqli_query($connectt,$sql) or die (mysqli_error($connectt));
                if (mysqli_num_rows($success) === 1){
                    mysqli_query($connectt,$sql) or die (mysqli_error($connectt));
                    mysqli_close ($connectt);

                    header("Location: index.php");
                }
                else {
                    echo "Wrong name!";
                }



        $stmt->free_result();
        $stmt->close();
        }
        }
    ?>

您可以告诉我这是一种登录形式. HTML不相关所以我不会发布它但我会发布包含连接文件.希望比我受过更多教育的人可以发现我看不到的错误,如果我遗漏了任何重要的错误.

包括:

<?php
$connectt = mysqli_connect("localhost","websecurity");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  ?>

星期五快乐!

解决方法

请尝试以下代码:

更新:

<?php  
if(isset($_POST["submit"])){
    //Connect
    $mysqli = new mysqli("localhost","websecurity");

    if($mysqli->connect_errno) {
        die("Connect failed: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error);
    }
    //Submitt pushed
    if(!empty($_POST['username']) && !empty($_POST['password'])) {
        $username= strip_tags($_POST['username']);
        $password= strip_tags($_POST['password']);

        $username = $mysqli->real_escape_string($username);
        $password = $mysqli->real_escape_string($password);


        // Prepare
        $sql = "SELECT username,password FROM login WHERE username = ? AND password = ?";
        $stmt = $mysqli->prepare($sql);
        $bind_result = $stmt->bind_param("ss",$username,$password);
        $stmt->execute();
        $stmt->store_result();
        if ($stmt->num_rows === 1){
            header("Location: index.php");
        }
        else {
            echo "Wrong name!";
        }


        $stmt->free_result();
        $stmt->close();
    }
}
?>

之前

<?php
if(isset($_POST["submit"])){
    //Connect
    $mysqli = new mysqli("localhost",password FROM login WHERE username = ? AND password = ?";
        $stmt = $mysqli->prepare($sql);

        if(!$stmt) {
            die("Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error);
        }

        //Prepared statements

        $bind_result = $stmt->bind_param("ss",$password);
        if(!$bind_result) {
            echo "Bind failed: (" . $stmt->errno . ") " . $stmt->error;
        }


        //Lets execute this

        //Give a value to statements
        $username =0;
        $password ='';

        $execute_result = $stmt->execute();
        if(!$execute_result) {
            echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
        }

        $stmt->bind_result($username,$password);



        $success = mysqli_query($connectt,$sql) or die (mysqli_error($connectt));
        if (mysqli_num_rows($success) === 1){
            mysqli_query($connectt,$sql) or die (mysqli_error($connectt));
            mysqli_close ($connectt);

            header("Location: index.php");
        }
        else {
            echo "Wrong name!";
        }



        $stmt->free_result();
        $stmt->close();
    }
}
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读