【问题标题】:cant send query with php mysqli prepared statement无法使用 php mysqli 准备好的语句发送查询
【发布时间】:2014-04-26 18:22:54
【问题描述】:

我的代码不起作用。我正在使用 php 和 mysqli 建立登录网站。我包括准备好的声明以提高安全性,但我认为我做错了。请帮忙。

它无法将信息发送到数据库。

“准备失败!”出现。有什么问题??

<?php

  if(isset($_POST['signup-name'], $_POST['signup-password-1'], $_POST['signup-password-2'], $_POST['signup-email-1'], $_POST['signup-email-2'], $_POST['signup-country'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field'])){
    if(!empty($_POST['signup-name']) and !empty($_POST['signup-password-1']) and !empty($_POST['signup-password-2']) and !empty($_POST['signup-email-1']) and !empty($_POST['signup-email-2']) and !empty($_POST['signup-country']) and !empty( $_POST['recaptcha_challenge_field']) and !empty( $_POST['recaptcha_response_field'])){

      echo"ok";

      $username = $_POST['signup-name'];
      $password1 = $_POST['signup-password-1'];
      $password2 = $_POST['signup-password-2'];
      $email1 = $_POST['signup-email-1'];
      $email2 = $_POST['signup-email-2'];
      $country = $_POST['signup-country'];

      //$recaptcha_challenge_field = $_POST['recaptcha_challenge_field'];
      //$recaptcha_response_field = $_POST['recaptcha_response_field'];
                if (filter_var($email1, FILTER_VALIDATE_EMAIL) && ($email1==$email2) && ($password1==$password2)) {
        include 'db_info.php';
        $mysqli = new mysqli("localhost", $db_uusseerrss, $db_ppwwdd, "user_db");
        if (mysqli_connect_errno()) {
          echo "no ok";
          printf("Connect failed: %s\n", mysqli_connect_error());
          exit();
        }

        $query = "INSERT INTO user_info (`username`, `email`, `password`, `country`) VALUES( ?, ?, ?, ?)";
        if ($stmt = $mysqli->prepare($query)) {
          $stmt->bind_param('ssss', $username, $email1, $hashed_password, $country );
          $stmt->execute();
          $stmt->close();
          //mysqli_close($link);
        }else{
          die('prepare() failed: ' . htmlspecialchars($stmt->error));
        }
      }else{
        echo "filter failed!";
      }
    }else{
      echo "value is not set";
  }

    }
  }

?>

【问题讨论】:

    标签: php sql mysqli prepared-statement


    【解决方案1】:

    您可以使用函数mysqli_error($mysqli) 显示您遇到的错误

    else{
       var_dump(mysqli_error($mysqli));exit;
       die('prepare() failed: ' . htmlspecialchars($stmt->error));
    }
    

    【讨论】:

      【解决方案2】:

      一个错误很明显

      INSERT INTO user_info (`username`, `email`, `password`, 'country')
      

      国家有单引号需要是

      INSERT INTO user_info (`username`, `email`, `password`, `country`)
      

      INSERT INTO user_info (`username`, `email`, `password`, country)
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 2018-12-11
      • 2011-06-20
      相关资源
      最近更新 更多