Struggling with something that should be very simple. I have three tables, one master table for plant readings, whose id is the foreign key that associates multiple readings from the compressor table and the ice temperatures table. I want to get the id from the plant reading, after it is created, to use as the foreign key when inserting data into the compressors and ice tables. But the code breaks around $plant_check_id. I can’t figure out why? lastInsertId() doesn’t seem to be working either?
When a new plant reading is created, first step is to create a new record with a unique ID
//create a new plant reading
$new_plant_reading = $connection->prepare("
INSERT INTO plant_readings (time_of_day) VALUES (:time_of_day)");
$plant_reading = $new_plant_reading->execute(
array(
':time_of_day' => $_POST["time_of_day"]
)
);
$last_id = $connection->prepare("
SELECT MAX(id) FROM `plant_readings`");
$plant_check_id = $last_id->query();
//$plant_reading_id = intval($plant_check_id->lastInsertId());
$statement = $connection->prepare("
INSERT INTO compressor_readings (suction, discharge, oil_pressure, oil_temperature, water_temperature, compressor_id, plant_readings_id) VALUES (:s, :d, :op, :ot, :wt, :cid, :pid)");
$result = $statement->execute(
array(
':s' => $_POST["suction1"],
':d' => $_POST["discharge1"],
':op' => $_POST["oil_pressure1"],
':ot' => $_POST["oil_temperature1"],
':wt' => $_POST["water_temperature1"],
':cid' => 1,
':pid' => $plant_check_id
)
);
Thanks in advance