1

The logic has me confused. I want to write one function to handle multiple forms. I have eight order forms, on different pages. They all use the same database. The function will need to handle insert functions OR update functions.

What I wrote before I got stumped:

// I might want to move this IF further down in the logic
// I wasn't sure what to put here in it's place.
if(isset($_POST['submitInsert']))
{

// setting up a loop to go through all the POST values
foreach($_POST as $name => $value) 
{ 
    // make sure POST values are set before wasting
    // any time doing stuff with them
    if($value!="")
    {

        // checking if the POST value is an array because
        // an array would have to be handled differently
        if(!is_array($value))
        {
            // Just printing info for now, Thinking maybe I should 
            // do an IF here to check for insert/update
            echo $name . " --- " . $value . "<br>";

        }
        else
        {
            // for an array, I'll need another foreach statement
            echo "$name"." --- ";
            print_r($value);
            echo "<br/>";
        }
    }
} 
}

More Info

I'm planning to switch between insert/update by naming the submit buttons 'submitInsert' or 'submitUpdate'. And after some data validation, check insert/update. After deciding this was the way to go, I realized I don't know how to start the function.

The form element names match the db field names, so the SQL query shouldn't be difficult.

Is there a better way to do this?

1
  • So, if I'm understanding you correctly, you are planning to use the same function to handle inserting and updating? Is there a reason for this? Commented Oct 2, 2014 at 14:49

1 Answer 1

2
if(isset($_POST['submit']))  
{
  //could use for loop but you know the fields you will be using for your sql.
  $id = $_POST['id'];
  $var1 = $_POST['var1']; 
  // Do a select see if it exists if it does use update else use insert.
  $query = "SELECT * from table where ID = $id";
  if($rowCount > 0)
  {
   $query = "Update table set var=$var1 where id = $id";
  }
  else
  {
   $query = "Insert into table (var1) VALUES ($var1)";
  }

}
Sign up to request clarification or add additional context in comments.

8 Comments

The only thing I'm wondering about: if a user tries to submit a new order with an order number that's already in the database, this function would just update the order. It wouldn't notify the user. Conversely a user trying to update an order that inputs a new order would just end up creating a new order. Is there a way to catch this?
The order number is unique, the database should generate it not the user, if the user has an order number then he can update, as well you don't let the user enter his order number, ideally he would select it from a predefined list.
My mistake for equating the order number to ($id). My system doesn't create the order numbers, another system does. Order numbers are scanned with one of those red laser scanners. So logically, the user is creating the order number and my system just records it. Order numbers will still be unique, but not an auto-incremented ID in the database.
Do you not have access to the database that generates the order numbers, if you do then you can simply cross reference? Easiest way is when the order number is generated it gets inserted into the table, then everything after is an update.
I don't have access to the other database.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.