inserting values from given quantity in database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
raj86
Forum Commoner
Posts: 25
Joined: Tue Sep 29, 2009 12:28 am

inserting values from given quantity in database

Post by raj86 »

Hello friends
i am working on a project in which i have to insert similar data multiple times, I have a register page for it. i.e
X Y Z 001
X Y Z 002
X Y Z 003
-
-
X Y Z 050
here only the number changes. is there any way by which i will only enter all the values???
here i will enter the values once and total quantity (50 in this case) in the database and it will automatically expanded in 50 rows.

thank you
mikecampbell
Forum Commoner
Posts: 38
Joined: Tue Oct 12, 2010 7:26 pm

Re: inserting values from given quantity in database

Post by mikecampbell »

You can insert multiple rows into a MySQL database with one command.

INSERT INTO table_name (fielda, fieldb, fieldc, quantity) VALUES ('X', 'Y', 'Z', 1), ('X', 'Y', 'Z', 2), ('X', 'Y', 'Z', 3), ...

You could build this with PHP like this.

Code: Select all

$sql = 'INSERT INTO table_name (fielda, fieldb, fieldc, quantity) VALUES ';
for ($i=1;$i<=50;$i++) 
{
   $sql .= "('X', 'Y', 'Z', $i), ";
}
$sql = substr($sql, 0, -2);
$result = mysql_query($sql);
Post Reply