jedeye Posted May 9, 2011 Share Posted May 9, 2011 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.. Quote Link to comment https://forums.phpfreaks.com/topic/235961-sql-statement-depeding-on-post-vars/ Share on other sites More sharing options...
fugix Posted May 9, 2011 Share Posted May 9, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235961-sql-statement-depeding-on-post-vars/#findComment-1213050 Share on other sites More sharing options...
spiderwell Posted May 9, 2011 Share Posted May 9, 2011 i would use one sql statement and with my statement have the variables default value as NULL and only fill them if the $_POST variable is not empty. note: NULL being a string saying NULL, not actually setting the variable to false or anything like that Quote Link to comment https://forums.phpfreaks.com/topic/235961-sql-statement-depeding-on-post-vars/#findComment-1213073 Share on other sites More sharing options...
Zane Posted May 9, 2011 Share Posted May 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235961-sql-statement-depeding-on-post-vars/#findComment-1213077 Share on other sites More sharing options...
Zane Posted May 9, 2011 Share Posted May 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235961-sql-statement-depeding-on-post-vars/#findComment-1213084 Share on other sites More sharing options...
jedeye Posted May 9, 2011 Author Share Posted May 9, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/235961-sql-statement-depeding-on-post-vars/#findComment-1213091 Share on other sites More sharing options...
Zane Posted May 9, 2011 Share Posted May 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235961-sql-statement-depeding-on-post-vars/#findComment-1213099 Share on other sites More sharing options...
jedeye Posted May 9, 2011 Author Share Posted May 9, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235961-sql-statement-depeding-on-post-vars/#findComment-1213105 Share on other sites More sharing options...
Zane Posted May 10, 2011 Share Posted May 10, 2011 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. } } Quote Link to comment https://forums.phpfreaks.com/topic/235961-sql-statement-depeding-on-post-vars/#findComment-1213118 Share on other sites More sharing options...
jedeye Posted May 10, 2011 Author Share Posted May 10, 2011 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.) Quote Link to comment https://forums.phpfreaks.com/topic/235961-sql-statement-depeding-on-post-vars/#findComment-1213128 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.