-1

I have this table structure:

TABLE test
id int(11) AUTO_INCREMENT
value1 text
value2 text
value3 int(11)

If I run this query:

SELECT value1,value2 FROM test WHERE value3=45

I'll get 2 rows as result.

I have this PHP code.

$stmt = $mysqli->prepare("INSERT INTO test (value1,value2) SELECT value1,value2 FROM test WHERE value3=45");
$stmt->execute();
$id = $stmt->insert_id;
$stmt->close();
echo $id;

The code will insert 2 rows because the selects gives 2 rows as result, right? But $id will only be the id of the first inserted row. How could I get the id of the following inserted row?

1 Answers1

-1

If you always know how many inserted rows there are, you could just get the max id and de-increment by the number of rows.

chaoskreator
  • 889
  • 1
  • 17
  • 39
  • That's awful, if there is an insert in the dead-time between INSERT and SELECT query I will make a mess. – user3781074 Jun 27 '14 at 12:10
  • Just going off the code provided. There doesn't show that there would be any other queries performed in between, so in that case, it would still be a viable solution. You could also get the insert_id of the first value and increment accordingly, depending on the number of inserted values. – chaoskreator Jun 28 '14 at 00:21