mysql - Drupal 7 函数 db_​​insert() 导致内部服务器错误 (500)

标签 mysql module drupal-7

我正在构建一种现场支付方式,除其他外,我需要在收到来自外部 API 服务的回调后将数据保存到自定义数据库表。

这是函数:

// callback from external service acting as client
function _api_callback() {
  global $user;
  $data = json_decode(file_get_contents("php://input"), $assoc = true);
  $date = $date['year'] . '-' . $date['month'] . '-' . $date['day'];
  $timestamp = strtotime($date);
  db_insert('postback')
  ->fields(array(
    'user_id' => $user->uid,
    'data' => $data,
    'created_date' => $timestamp,
  ))
  ->execute(); // save data from external service
}

在网站的访问日志中,我可以看到当外部回调到达时,我的网站会响应 500 代码。

但是,如果我注释掉该函数中的所有内容,外部回调会收到 200 响应代码。

因此,当收到此回调时,出现了一些问题。由于来自外部 API 服务的回调会启动一个全新的 session ,因此很难调试。

自定义表“postback”已在自定义模块的 .install 文件中成功创建,并且我已在 phpMyAdmin 中检查该表是否存在且状态良好。这是 .install 文件中的 schema() 函数:

function module_payments_schema() {
  $schema['postback'] = array(
    'description' => 'Data saved from API postback URL.',
    'fields' => array(
      'id' => array(
        'description' => 'Auto incremental id.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE),
      'user_id' => array(
        'description' => 'User id for the order.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE),
      'order_id' => array(
        'description' => 'Current order number.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      'data' => array(
          'description' => 'Postback data from external API.',
          'type' => 'varchar',
          'length' => 1020,
          'not null' => TRUE,
          'default' => ''),
      'created_date' => array(
          'description' => 'Created date and time (yyyy-mm-dd H:i:s).',
          'type' => 'varchar',
          'mysql_type' => 'DATETIME',
          'not null' => TRUE,
        ),
      ),
      'primary key' => array('id'),
    );

    return $schema;
  }

谁能看出问题所在吗?

最佳答案

事实证明我需要像这样格式化日期值......

date('Y-m-d H:i:s');

...为了使用 mysql DATETIME 类型。

所以工作函数现在看起来像这样:

function _api_callback() {
  global $user;
  $data = json_decode(file_get_contents("php://input"), $assoc = true);
  db_insert('postback')
  ->fields(array(
    'user_id' => $user->uid,
    'data' => $data,
    'created_date' => date('Y-m-d H:i:s');
  ))
  ->execute(); // save data from external service
}

关于mysql - Drupal 7 函数 db_​​insert() 导致内部服务器错误 (500),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58099287/

相关文章:

python - 来自多个模块的全局变量对执行代码不可见?

mysql - 如何管理数据库中的用户角色?

php - OO PHP 和 AJAX 表单验证

perl - 如何在与脚本相同的目录中找到 Perl 模块

module - OCaml 在实现中重复整个签名

php - Drupal 7 Forms API 条件逻辑在 IE 中不起作用

drupal - 基于规则的节点创建 : commerce product + product display node set

php - 如何使用 AJAX 和 json 从 PHP Mysql 中提取 2 个结果

mysql - 如何使用 SQLite 创建数字未知的事件表

drupal - 如何使用 module_load_include 重用 Drupal 模块代码