【问题标题】:Error 500 INSERT PDO with ajax错误 500 使用 ajax 插入 PDO
【发布时间】:2015-05-31 17:29:38
【问题描述】:

我的recordDate.php:

<?php
     $host = "localhost";
     $userDB = "user";
     $passwordDB = "0000";
     $database = "myDatabase";

     $getUser = $_POST['user'];

     $now = date("h:i:s");

     $pdo = new PDO('mysql:host='.$host.';dbname='.$database, $userDB, $passwordDB);
     $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

     $stmt = $pdo->prepare("INSERT into log (userLog,dateLog) VALUES (:userLog,:dateLog)");
     $stmt->bindParam(':userLog', $getUser);
     $stmt->bindParam(':dateLog', $now);
     $stmt->execute();
?>

我不明白,因为它用这个 ajax 显示 500 Internal Server Error

$.ajax({
        type: "POST",
        url: "recordDate.php",
        data: "user="+user,
}); 

数据连接很好,并且可以与 SELECT 一起正常工作。 有人帮帮我吗?

【问题讨论】:

  • 在 ajax 中使用数据,如 data : {user:user}
  • 这是一个内部服务器错误,调试PHP,打开错误报告并检查网络选项卡是否有错误,AJAX脚本没有问题,如果它不为空,还要检查user

标签: php ajax pdo


【解决方案1】:

你的 ajax 调用应该这样格式化:

$.ajax({
    type: "POST",
    url: "recordDate.php",
    data: {user: user}, // you need to pass an object here
    sucess: function() {},
    error: function() {}
});  

【讨论】:

    【解决方案2】:

    我正在使用您的代码,它在我的计算机上看起来不错。

    请检查您的错误日志。如果您不知道错误日志路径是什么,请使用&lt;?php phpinfo();?&gt; 创建一个文件并搜索“error_log” 或在您的文件中包含以下代码。

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    

    检查:

    1. 数据库名称
    2. 数据库用户
    3. 数据库密码
    4. 数据库主机名
    5. 列名

    500 Internal Server Error 是您的 PHP 的错误

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-23
      • 2017-03-20
      • 2018-09-11
      • 2015-04-17
      • 2023-03-19
      • 2016-03-27
      • 1970-01-01
      相关资源
      最近更新 更多