Page 1 of 1

inserting values from given quantity in database

Posted: Mon Oct 25, 2010 11:26 pm
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

Re: inserting values from given quantity in database

Posted: Tue Oct 26, 2010 1:53 pm
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);