Insert 2nd auto increment id for multiple rows

Viewed 433

I want to insert same auto increment id for multiple rows. Currently,I am using the default auto increment is. how can I add 2nd auto increment id (same id ) for multiple rows in another column? As I am using groupid in the following code. .

$data = array();
foreach ($_POST['array'] as $key => $array) {
    $data[$key]['groupid'] = intval($array['groupid']);
    $data[$key]['User'] = strip_tags(trim(strval($array['user'])));
    $data[$key]['address'] = strip_tags(trim(strval($array['address'])));

}
$stmt = $pdo->prepare("INSERT INTO users ('groupid','user','address') VALUES (:groupid, :user, :address) ");
foreach ($data as $key => $array) {
    $stmt->execute(array(
        ':groupid' => $array['groupid'],
        ':user' => $array['user'],
        ':address' => $array['address'],
    ));
}
1 Answers

What you're asking for is beyond the scope of auto_increment. I don't know of an SQL feature that allows this, you would need to write the logic yourself in the application.

Try this:

  • SELECT MAX(groupid) FROM users
  • Save the result as a PHP variable
  • Add one (+1) to that variable
  • Use the variable in your INSERT statement for the groupid.
Related