The scenario is I have two table, we can call it table_a and table_b. one have 4 column, and the other one has 3, here is what they look like
table_a table_b
+------+------+-----+----+ +-----+-----+-----+
| a_id | name | age |sex | |b_id |a_id |type |
+------+------+-----+----+ +-----+-----+-----+
I have a query that insert a data to the two table at the same time
$query = "INSERT INTO table_a SET name=:name, age=:age, sex=:sex; INSERT INTO table_b SET type=:type";
$stmt = $this->conn->prepare($query);
$this->name = $this->name;
$this->name = $this->age;
$this->name = $this->sex;
$this->name = $this->type;
$stmt->bindParam(':name', $this->name);
$stmt->bindParam(':age', $this->age);
$stmt->bindParam(':sex', $this->sex);
$stmt->bindParam(':type', $this->type);
if($stmt->execute()){
return $this->conn->lastInsertId();
}else{
return -1;}
a_id on table_a is autoincrement similar to b_id in table_b, name, age, sex, type were all inserted by user using
$_POST
Now, my inquiry, is there a way I can automatically put a_id value from table_a to a_id in table_b? The lastInsertId is working properly, though Im not sure how I can put it on the other table. I am also thinking about another solution, and that is assigning a value to a_id using this
a_id=:LAST_INSERT_ID()
But I am not sure how to do that or if it is possible.