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
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}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.