phpretard Posted May 22, 2008 Share Posted May 22, 2008 I am trying to add 5 rows to a table with one if statement. The only variable that is different: $Pname I know the code below will work...but is there a way to do this without 5 querys? (NO ARRAYS PLEASE) if ($Pname=='5_Item_Package'){ mysql_query("INSERT INTO projects (id, Cnumber, Pname, date, approved, status, qty, cost, included, description, hourly, locked ) VALUES ('', '$Cnumber', 'DIFFERENT NAME 1', '$today', '$approved', '$status', '$qty', '$cost', '$included', '$description', '$hr', '$locked' )"); mysql_query("INSERT INTO projects (id, Cnumber, Pname, date, approved, status, qty, cost, included, description, hourly, locked ) VALUES ('', '$Cnumber', 'DIFFERENT NAME 2', '$today', '$approved', '$status', '$qty', '$cost', '$included', '$description', '$hr', '$locked' )"); mysql_query("INSERT INTO projects (id, Cnumber, Pname, date, approved, status, qty, cost, included, description, hourly, locked ) VALUES ('', '$Cnumber', 'DIFFERENT NAME 3', '$today', '$approved', '$status', '$qty', '$cost', '$included', '$description', '$hr', '$locked' )"); mysql_query("INSERT INTO projects (id, Cnumber, Pname, date, approved, status, qty, cost, included, description, hourly, locked ) VALUES ('', '$Cnumber', 'DIFFERENT NAME 4', '$today', '$approved', '$status', '$qty', '$cost', '$included', '$description', '$hr', '$locked' )"); mysql_query("INSERT INTO projects (id, Cnumber, Pname, date, approved, status, qty, cost, included, description, hourly, locked ) VALUES ('', '$Cnumber', 'DIFFERENT NAME 5', '$today', '$approved', '$status', '$qty', '$cost', '$included', '$description', '$hr', '$locked' )"); } else {Just one query} Quote Link to comment Share on other sites More sharing options...
littledragon Posted May 22, 2008 Share Posted May 22, 2008 Yes, just repeat the VALUES() brackets for each insert, seperated by comma INSERT INTO `database`.`table` ( `col1` , `col2` , ) VALUES ( 'hello', 'hello2' ), ( 'hello3', 'hello4' ); Quote Link to comment Share on other sites More sharing options...
phpretard Posted May 22, 2008 Author Share Posted May 22, 2008 So basically do the same thing without the "INSERT INTO" statement repeated? Quote Link to comment Share on other sites More sharing options...
littledragon Posted May 22, 2008 Share Posted May 22, 2008 Yeah, this makes it a single statement. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 22, 2008 Share Posted May 22, 2008 Just a couple other points: 1. There is no need to specify id in the field list. Since you are passing an empty string I am surmising that the field is an auto-increment field. You don't need to include it in the INSERT statement to work correctly. If those really are your queries you can save a lot of time by creating the query as a variable first. It is also extremely helpful to create your queries as variables as you can display them if there is an error. I know you said no arrays, but this is so much cleaner. <?php if ($Pname=='5_Item_Package'){ for ($i=1; $i<=5; $i++) { $values[] = "('$Cnumber', 'DIFFERENT NAME $i', '$today', '$approved', '$status', '$qty', '$cost', '$included', '$description', '$hr', '$locked' )"; } $query = "INSERT INTO projects (Cnumber, Pname, date, approved, status, qty, cost, included, description, hourly, locked) VALUES " . implode(', ', $values); mysql_query($query) or die (mysql_error() . "<br>Query:<br>$query"); } else {Just one query} ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.