Drupal 7函数db_insert()导致内部服务器错误(500)

问题描述 投票:0回答:1

我正在构建一种现场付款方式,在从外部API服务接收到回调之后,我还需要将数据保存到自定义db表中。

这是功能:

// 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个回答
0
投票

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

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
}
© www.soinside.com 2019 - 2024. All rights reserved.