Jump to content

Adding Multiple Rows With One IF


phpretard

Recommended Posts

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}

Link to comment
https://forums.phpfreaks.com/topic/106730-adding-multiple-rows-with-one-if/
Share on other sites

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}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.