Jump to content

Recommended Posts

Alright, so I am writing a script for a secretary to do some data entry. With this script there is a few file upload fields, now depending on the item entered not all fields need to be filled out. Therefore, the SQL statement will change depending on whether or not that field was filled out.

 

Should I just use IF statements to check too see if the POST var was sent, then change the statement accordingly, or would there be a more productive way?

 

Thanks for any feedback.. :)

Link to comment
https://forums.phpfreaks.com/topic/235961-sql-statement-depeding-on-post-vars/
Share on other sites

Alright, so I am writing a script for a secretary to do some data entry. With this script there is a few file upload fields, now depending on the item entered not all fields need to be filled out. Therefore, the SQL statement will change depending on whether or not that field was filled out.

 

Should I just use IF statements to check too see if the POST var was sent, then change the statement accordingly, or would there be a more productive way?

 

Thanks for any feedback.. :)

that's what i normally do, if not all of the data is required I set up if statements to see which data is passed an i insert that data in to the db

Typically, in situations like this I normally add an extra variable to the POST variables mix.  I put a hidden variable in there.  Mainly to tell me which type of data I'm receiving.

 

Depending on the way you are POSTing your data, you have a few different ways to go about this technique. 

One of these ways is to POST the form with a GET variable.

</pre>
<form method="'post'" action="'awesomescript.php?o=1'">
.....
...various form elements and a submit button.
.....
</form>
<b

Notice how the action has a querystring attached to it.  Doing that, you can check for it in awesomescript.php, obviously, for $_GET['o'].  I usually choose a single letter for such hidden things just to have that sense of security... .. obscurity is what you'd call it really.

 

Secondly, you can use an input type hidden inside your form.

 

And last of all, is the AJAX way.  If you're not using AJAX then don't worry about it, but if you are.. it's no different than just adding onto the querystring an extra variable.

Should I just use IF statements to check too see if the POST var was sent, then change the statement accordingly, or would there be a more productive way?

The most efficient way IMO, would be a switch using that hidden variable I mentioned.

thanks for you're replies guys...

 

Spider: you're idea is good, but I thought of that ;)

 

Zanus: Excellent, thanks for completing my thought... I was thinking about using a select case switch() statement, but couldn't really come up with a good way of using it. I'll also look into that AJAX part... I have been playing around with a bit of jQuery on the design end, and want to start incorporating it into my scripts.

 

I have a question.. on this script I've been talking about.. Some of the files that would be uploaded are quite large, I know how to setup the php.ini to allow for bigger files, however, instead of just showing a blank browser while the script is running the queries and uploading the data. If I used something similar to "document.ready()" function in an if statement I could show a "Loading.." graphic while those queries are running in the background?

If I used something similar to "document.ready()" function in an if statement I could show a "Loading.." graphic while those queries are running in the background?

If you mean to upload via AJAX, then yes.. that's ideally how you'd do it.  No one wants to see a blank screen for half an hour or more.

 

The only other way I can possibly think of is to use Flash and that's a different story altogether... even then, you'd be passing variables to an external upload script.. just like AJAX.

Back on the original topic... i have 3 of these 'possible' POST vars.. so I would need 3 'hidden' fields in order to check for them?

Because var1 could be used, and the other 2 vars not used... then again, there could be var1 and var3 used then not var2.

 

I guess I'm a bit confused on using the switch() func

 

 

 

 

The main part you're forgetting here is that there is a PHP function called isset that one really must use to work with REQUEST (GET, POST, SERVER) variables.

 

If this is only one form that you're using, and are doing separate things depending on which fields weren't filled in, then you probably don't need a hidden variable.  Though you will need the isset funciton no matter what you do if you're accessing POST, GET variables.

 

Assuming you don't need a hidden variable.. you would check for the submit button.

if(isset($_POST['submit'])) {
// The form was submitted... now what.
}

Now you can put your logic inside this depending on what was or wasn't inputted.  Just off the top of my head, I would go about this in checking for the most inputted first.

if(isset($_POST['submit'])) {
// The form was submitted... now what.
     if(isset($_POST['var1']) && isset($_POST['var2']) && isset($_POST['var3'])) {
            ////  All three were inputted.
     } else if (isset($_POST['var2']) && isset($_POST['var3'])) {
            /// Only var2 and var3 were inputted
     } else if(isset($_POST['var3'])) {
            ///  Only var3 was inputted.
     } else {
            /// Nothing was inputted.
     }
          
}

I'm aware of the isset() function and what you just posted is how I have already approched it, but i guess I'm just trying to over complicate my code by not using if, else statements

 

 

i was thinking something along the lines of:


chk_var = $_GET['o'];

if ( isset($chk_var) && $chk_var == "1" ( {

switch ($chk_var) {
  case "value1":
  // set sql statment
  case "value2":
// set sql statement

}

} else {
// no value continue

 

In my head though I think I'm trying to over complicate it, or just not grasping everything that I should. As I am stuck trying to figure out, how put my theory into actions through that switch() statement, and putting that into a sql statement. Actually, how to append those values from the form input into the sql statement due to the amount of sequences that are involved. (ie: var1 & var3, then only placing those in a sql statement, or var3 only then to the sql statement, or var2 and var3 then into a sql statement, or var1& var2 and into a sql statement, etc.)

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.