【问题标题】:PDO/MySQL: Inserting with a prepared statement - 500 (Internal Server Error)PDO/MySQL:使用准备好的语句插入 - 500(内部服务器错误)
【发布时间】:2018-06-23 12:00:59
【问题描述】:

我正在用 PHP 和 MySQL 编写一个简单的数据库应用程序。我正在使用 ajax 在服务器端运行脚本。我已经使用 PDO 编写了一个数据库类。

我编写了各种 php 脚本来处理插入数据,它们都在使用我的类的“prepare()”和“execute()”方法。

我编写了一个单独的 php 脚本来创建表和删除表,以及插入示例数据和删除所有数据。这是为了帮助我在构建和调试数据库应用程序时返回“已知”数据集。

我在 html 中创建了四个按钮,我异步调用了一个 php 脚本,使用 GET 传递了一个名为“script”的变量。在我的脚本中,我有一个 switch 语句来确定要运行的 SQL 命令。没有我需要绑定的变量。

当我点击一个按钮来执行一个脚本时,它可以工作——如果情况允许的话。因此,例如,如果我单击删除所有表,它将删除这些表。但是,如果我再次单击它(并且没有要删除的表),javascript 会返回 500 - 内部服务器错误。我想返回一条错误消息,但不知道在我的脚本中处理这个问题的位置。

这是我的类方法:

public function prepare($query){
  // PDO prepare allows for binding of values, removes threat of SQL injection and improves query performance
  $this->statement = $this->connection->prepare($query);
}

public function bind($parameter, $value, $type = null){
  // PDO bindValue binds inputs to placeholders
  if(is_null($type)){
    switch(true){
      case is_int($value):
        $type = PDO::PARAM_INT;
        break;
      case is_bool($value):
        $type = PDO::PARAM_BOOL;
        break;
      case is_null($value):
        $type = PDO::PARAM_NULL;
        break;
      default:
        $type = PDO::PARAM_STR;
    }
  }
  $this->statement->bindValue($parameter, $value, $type);
}

public function execute(){
  // Executes the prepared statement
  return $this->statement->execute();
}

public function allresults(){
  // Returns all results
  $this->execute();
  return $this->statement->fetchAll(PDO::FETCH_ASSOC);
}

这里是使用 ajax 调用的 php 脚本的关键部分:

switch ($script) {
  case "create":
      global $create_tables;
      global $database;
      $database->prepare($create_tables);
      $result = $database->execute($create_tables);
      if($result){
        echo "Success";
      }
      else{
        echo "Failed";
      }
      break;

其中一个按钮:

<button type="button" class="btn btn-success" onclick="library('newsql.php?script=create')">Create Tables</button>

还有我的 js:

function library(path) {


xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
          document.getElementById("main").innerHTML = this.responseText;
      }
  };
  xmlhttp.open("GET",path,true);
  xmlhttp.send();
};

我认为这与准备好的语句的标准返回有关,但也许与执行没有绑定变量的准备好的语句或重复准备好的语句而不更改变量有关?

【问题讨论】:

    标签: php mysql ajax pdo


    【解决方案1】:

    所以你在诊断发生了什么方面遇到了问题。

    • 在您的 JS 函数中,您正在处理状态 == 200 但没有处理错误(例如 500)
    • 在浏览器中打开您的开发控制台 (F12) 并转到“网络”选项卡,然后单击按钮以获取 500 错误,您将能够看到 ajax 调用的标题和响应选项卡(请求/response 标头和 html/json/whatever 响应),即您的 PHP
    • 作为 PHP 的 500 错误并且您在响应中看不到任何内容,那么您的 PHP 脚本没有回显任何内容,但您可能会在 PHP 或 Apache 的错误日志中看到一些内容。当出现错误 500 时,您必须学会在每次调用时监控它们。然后,您将看到调用错误 500 而不是您在 JS 函数中期望的 200(OK)的确切原因...

    希望对您有所帮助。

    【讨论】:

    • 这有帮助。我检查了我的 php 错误日志,并注意到由于我没有做任何事情来捕获我的 PDO 执行方法上的异常,因此它的行为与预期不符。我用 try-catch 修改了我的方法,它现在返回正确的错误消息,而不是内部服务器错误。感谢您的帮助。
    猜你喜欢
    • 2016-01-12
    • 2013-09-21
    • 2015-10-28
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多