Search code examples
mysqldatabaseauto-increment

MySQL: How to insert multiple records with same ID autoincrement


I have a table with 1 column that stores the ID auto_increment and with that instruction:

$query = "SELECT first_data, second_data, third_data
            FROM table_data
                WHERE condition = 'value_condition'";
$result = mysql_query($query) or die(mysql_error());

$no_of_rows = mysql_num_rows($result);

    if ($no_of_rows > 0) {
        while($row = mysql_fetch_assoc($result)) {
            $valueone = $row['first_data'];
            $valuetwo = $row['second_data'];
            $valuethree = $row['third_data'];

            $queryTwo = "INSERT INTO historial_ventas (reg1, reg2, reg3) 
                        VALUES('$ivalueone','$valuetwo','$valuethree')";
            $resultTwol = mysql_query($queryTwo) or die(mysql_error());
        }
    } else {
        return false;
}

Information is stored as follows:

ID(auto_increment)  reg1      reg2      reg3
______________________________________________
1                   value1    value2    value3
2                   value1    value2    value3
3                   value1    value2    value3
4                   value1    value2    value3

But I want the records maintained in the while loop, stored with the same ID as in a purchase online, ie you save the products and their features but with the same ID to buy, as this way:

ID(auto_increment)  reg1      reg2      reg3
______________________________________________
1                   value1    value2    value3
1                   value1    value2    value3
1                   value1    value2    value3
1                   value1    value2    value3

Solution

  • The normalized way is to use a joint table like this:

    Purchase System

    Both PurchaseId and ItemId are set to auto-increment. The purpose of a Primary Key is to uniquely identify each row.

    Updated: Customer

    Read up on Database Normalization on how you can design your database to be effective.

    enter image description here