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
inserting values from given quantity in database
Moderator: General Moderators
-
mikecampbell
- Forum Commoner
- Posts: 38
- Joined: Tue Oct 12, 2010 7:26 pm
Re: inserting values from given quantity in database
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.
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);