简体   繁体   English

PDO插入查询不适用于整数

[英]PDO Insert Query Not Working For Integer

I have this code to insert a new row into MySQL DB using PDO: 我有以下代码使用PDO将新行插入MySQL数据库:

$query = INSERT INTO asset_positions (pos_asset_id, pos_latitude, pos_longitude, pos_timestamp) VALUES (:pos_asset_id, :pos_latitude, :pos_longitude, :pos_timestamp)
$statement = $pdo->prepare($query);
$array = [':pos_asset_id' => 1, ':pos_latitude' => -8.5, ':pos_longitude' => 125.5, ':pos_timestamp' => 1398160487];
$statement->execute($array);
echo $pdo->lastInsertId();

The query runs without any error shown. 查询运行时未显示任何错误。 The newly inserted row ID is echoed. 回显新插入的行ID。 However, when i look in the DB, it only insert the latitude, longitude and timestamp. 但是,当我查看数据库时,它仅插入纬度,经度和时间戳。 The pos_asset_id field in the newly inserted row is empty. 新插入的行中的pos_asset_id字段为空。

Could somebody point out where is the problem? 有人可以指出问题出在哪里吗? There is no error message displayed. 没有显示错误信息。 I've been trying to solve this for hours without avail. 我一直在努力解决这一问题,无济于事。

Ps. 附言 This is my first time using PDO, so please bear with me. 这是我第一次使用PDO,请多多包涵。 Thanks. 谢谢。

EDIT 编辑

Solved! 解决了! I didn't notice that there's a FK relation between asset_positions.pos_asset_id and asset.asset_id . 我没有注意到asset_positions.pos_asset_idasset.asset_id之间存在FK关系。 Once i remove this relationship constrains, the INSERT works properly now, the pos_asset_id value is inserted to the record. 一旦消除了这种关系约束,INSERT现在即可正常工作,将pos_asset_id值插入到记录中。

Anyway, thanks all! 无论如何,谢谢大家! :) :)

try running with error catching, it will give you better understanding of what is happening. 尝试运行时捕获错误,它将使您对正在发生的事情有更好的了解。

try {
   $stmt->execute();
} catch (PDOException $p) {
    echo $p->getMessage();
} catch (Exception $e) {
    echo $e->getMessage();
}

But your common sense and user3540050 are right... it's a column related issue probably 但是您的常识和user3540050是正确的...这可能是与列相关的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM