【问题标题】:Drupal 7 function db_insert() causes internal server error (500)Drupal 7 函数 db_​​insert() 导致内部服务器错误 (500)
【发布时间】:2019-09-25 13:13:30
【问题描述】:

我正在构建一种现场支付方式,除此之外,我需要在收到来自外部 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 服务的回调会启动一个全新的会话,因此很难调试。

自定义表“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;
  }

谁能看出问题所在?

【问题讨论】:

    标签: mysql module drupal-7


    【解决方案1】:

    原来我需要像这样格式化日期值...

    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
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-07
      • 1970-01-01
      • 2013-08-11
      • 2012-03-29
      • 2014-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      相关资源
      最近更新 更多