简体   繁体   中英

PDO Insert Query Not Working For Integer

I have this code to insert a new row into MySQL DB using PDO:

$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. 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.

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. Thanks.

EDIT

Solved! I didn't notice that there's a FK relation between asset_positions.pos_asset_id and asset.asset_id . Once i remove this relationship constrains, the INSERT works properly now, the pos_asset_id value is inserted to the record.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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