phppup Posted February 27, 2012 Share Posted February 27, 2012 I need to update fields through a form, but there are MANY fields. Instead of UPDATE table SET email='$email', name, etc, for EVERY value, is there a shortcut that might loop through all my fields, since I want every one of them to be checked for updated info. Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/ Share on other sites More sharing options...
Zane Posted February 27, 2012 Share Posted February 27, 2012 http://www.phpfreaks.com/forums/index.php?topic=354422.msg1674005#msg1674005 Yes, there's a way to loop through them all without listing them separately. First of all though, avoid manually creating variables that only hold the value of another. $roastturkey = $_POST['roastturkey']; Has no benefit whatsoever other than aesthetics. Gee, now I only have to type $broccoli instead of $_POST['broccoli']... how l337!! That is a pointless concept unless you have calculations involved. You wouldn't pour your coffee from your coffee cup into another coffee cup just to add cream to it would you.... and then again for sugar? To loop through your inputs, you need to know two things. 1 - Do you want everything in $_POST? 2 - What don't you need from $_POST? ... Yes I realize those are the same, but a little repetition never hurt anyone. Using a foreach loop, you can grab EVERYTHING from $_POST and append it to a variable. For example, assuming you wanted EVERYTHING inside $_POST $myUpdateList = array(); foreach($_POST as $indexName => $value) { $myUpdateList[] = "$indexName = \"$value\""; } This will yeild an array containing all of your fields and their new values..Everything in that array will look something like this broccoli = "burnt" Now, the task is to take that array and implode it into a comma separated list.. $fields = implode(",", $myUpdateList); All that's left is the actual UPDATE statement now... and you have everything you need for it inside $fields UPDATE yourTable SET $fields WHERE id = $id Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321791 Share on other sites More sharing options...
phppup Posted February 27, 2012 Author Share Posted February 27, 2012 well, I'm glad you're here, because I'm doing something wrong. I posted it as written: $testtable = array();foreach($_POST as $indexName => $value) { $myUpdateList[] = "$indexName = \"$value\"";} $fields = implode(",", $testtable); $sql=("UPDATE testtable SET $fields WHERE id = $record_id "); but I was getting a T-string error Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321794 Share on other sites More sharing options...
Zane Posted February 27, 2012 Share Posted February 27, 2012 The only way there could be a T_STRING error is if you have quotes/apostrophes in your input. Otherwise, I have no idea why there is a T_STRING error. The double quotes are escaped just fine. If you do have apostrophes in your inputs, then take a look at the addslashes function... that should fix it. ALSO, you are not populating your $testtable array. So the $fields variable holds literally nothing. replace $myUpdateList with $testtable Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321797 Share on other sites More sharing options...
phppup Posted February 27, 2012 Author Share Posted February 27, 2012 Got rid of the T-string error. There are no MySQL errors, but the page takes me to MY own ERROR message rather than SUCCESS after submission. SO it's missing something. I changed the table name to MY testtable name. Are all others AS IS? Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321800 Share on other sites More sharing options...
Zane Posted February 27, 2012 Share Posted February 27, 2012 Got rid of the T-string error. There are no MySQL errors, but the page takes me to MY own ERROR message rather than SUCCESS after submission. SO it's missing something. I changed the table name to MY testtable name. Are all others AS IS? Post the code that you have.. My crystal ball is broken Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321802 Share on other sites More sharing options...
phppup Posted February 27, 2012 Author Share Posted February 27, 2012 if(isset($_POST['action']) && $_POST['action'] == 'submitform') { //recieve the variables $roastturkey = $_POST['roastturkey']; $broccoli = $_POST['broccoli']; $brisket = $_POST['brisket']; $carrots = $_POST['carrots']; $record_id = (isset($_POST['record_id'])) ? $_POST['record_id'] : ''; if(!empty($record_id)){ $testtable = array();foreach($_POST as $indexName => $value) { $myUpdateList[] = "$indexName = \"$value\"";} $fields = implode(",", $testtable); $sql=("UPDATE testtable SET $fields WHERE id = $record_id "); $result=mysql_query($sql); } if($result){ echo "Successful"; } else { echo "ERROR"; } EDITed for CODE tags. Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321806 Share on other sites More sharing options...
Zane Posted February 27, 2012 Share Posted February 27, 2012 1.) You did not do as I suggested earlier you are not populating your $testtable array. So the $fields variable holds literally nothing. replace $myUpdateList with $testtable $testtable = array();foreach($_POST as $indexName => $value) { $myUpdateList[] = "$indexName = \"$value\"";} 2.) You receive YOUR "ERROR" message because the query failed.. 3.) Why is your SQL query inside parenthesis? 4.) I'll bet you all of my life savings that your query failed because you used every single element in your POST array Put this code underneath your $sql line... and post what it returns here echo "", print_r($_POST), ""; While you're at it.... echo your $sql variable and post that here as well. Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321807 Share on other sites More sharing options...
phppup Posted February 27, 2012 Author Share Posted February 27, 2012 No messages. Just the same ERROR message from my own script. So is it not grabbing the fields, or is it not grabbing the record_id number? Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321811 Share on other sites More sharing options...
Zane Posted February 27, 2012 Share Posted February 27, 2012 Try again echo "", print_r($_POST), ""; Echoing that and your $sql variable should indeed output something. Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321815 Share on other sites More sharing options...
phppup Posted February 27, 2012 Author Share Posted February 27, 2012 Not sure if I was posting it correctly, so i stuck it in 3 different places, and NO MESSAGES Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321823 Share on other sites More sharing options...
Zane Posted February 27, 2012 Share Posted February 27, 2012 Post the code.. post the code, post the code. This is the last time I'll ask this. That snippet should have printed something out.. Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321824 Share on other sites More sharing options...
phppup Posted February 27, 2012 Author Share Posted February 27, 2012 AFTER CONNECTION: //save the data on the DB and send the email if(isset($_POST['action']) && $_POST['action'] == 'submitform') { //recieve the variables $roastturkey = $_POST['roastturkey']; $broccoli = $_POST['broccoli']; $brisket = $_POST['brisket']; $carrots = $_POST['carrots']; } $record_id = (isset($_POST['record_id'])) ? $_POST['record_id'] : '';//check for $record_id emptiness if(!empty($record_id)){ echo "<pre>", print_r($_POST), "</pre>"; $testtable = array();foreach($_POST as $indexName => $value) { $testtable[] = "$indexName = \"$value\"";} $fields = implode(",", $testtable); $sql=("UPDATE testtable SET $fields WHERE id = $record_id "); echo "<pre>", print_r($_POST), "</pre"; echo "$sql"; $result=mysql_query($sql); } if($result){ echo "Successful"; } else { echo "ERROR"; } Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321825 Share on other sites More sharing options...
Zane Posted February 27, 2012 Share Posted February 27, 2012 Ok, so you have the snippet in there... albeit in the right spot. I can't begin to fathom why it doesn't output ANYTHING.. This is beyond me now. One thing you can try it to put this error_reporting(448191); ini_set("display_errors", true); at the VERY TOP of your script... RIGHT below the <?php tag Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321829 Share on other sites More sharing options...
phppup Posted February 27, 2012 Author Share Posted February 27, 2012 Notice: Undefined variable: result in... line 91 This correcsponds to my: if($result){ towards the end. Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321831 Share on other sites More sharing options...
Pikachu2000 Posted February 27, 2012 Share Posted February 27, 2012 I can't begin to fathom why it doesn't output ANYTHING.. This is beyond me now. It's inside a conditional that checks if isset($_POST['record_id']), so the conditional has to be evaluating FALSE. Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321833 Share on other sites More sharing options...
Zane Posted February 27, 2012 Share Posted February 27, 2012 $record_id must be empty. $result is only created if $record_id isn't empty. Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321835 Share on other sites More sharing options...
phppup Posted February 27, 2012 Author Share Posted February 27, 2012 OKAY, I pulled out the conditional and it printed: Array ( [roastturkey] => 2.00 [broccoli] => 0.00 [brisket] => 6.00 [carrots] => 0.00 [submitpass] => Submit Order ) 1 so now the question is, How will it know which row ID to post the new data to? Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321839 Share on other sites More sharing options...
Pikachu2000 Posted February 27, 2012 Share Posted February 27, 2012 Exactly. How will it know if you don't tell it? Oh, and what is the purpose of this? It appears to do precisely nothing. //recieve the variables $roastturkey = $_POST['roastturkey']; $broccoli = $_POST['broccoli']; $brisket = $_POST['brisket']; $carrots = $_POST['carrots']; And the closing curly brace that immediately follows that code block should be moved to the end; after the query execution. Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321841 Share on other sites More sharing options...
phppup Posted February 27, 2012 Author Share Posted February 27, 2012 The PHP book said to put it there, so I put it there. Meanwhile, I need to get the UPDATES into the correct record ID using the form. Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321843 Share on other sites More sharing options...
Pikachu2000 Posted February 27, 2012 Share Posted February 27, 2012 This is all code from a book? Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321844 Share on other sites More sharing options...
phppup Posted February 27, 2012 Author Share Posted February 27, 2012 nO, NO , NO. I'm saying that a book said to have that post info on top of page. Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321846 Share on other sites More sharing options...
phppup Posted February 27, 2012 Author Share Posted February 27, 2012 I originally had a if(isset($_POST['action']) && $_POST['action'] == 'submitform') { itemized items } but took it out while trying to de-bug this scripting Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321847 Share on other sites More sharing options...
AyKay47 Posted February 28, 2012 Share Posted February 28, 2012 Alright, you asked for my help. So you are going to have to post the relevant updated code along with the issue, as it appears that Zane has pretty much already answered this and Pika helped. Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321902 Share on other sites More sharing options...
Drummin Posted February 28, 2012 Share Posted February 28, 2012 Seems like what you are trying to do is being over complicated by creating an array from a post array and I see no filters for record_id or action posted values. This is assuming of course that there are not any other hidden values being passed with POST and all form names match table field names. <?php if(isset($_POST['action']) && $_POST['action']=="submitform"){ //recieve the variables $roastturkey = $_POST['roastturkey']; $broccoli = $_POST['broccoli']; $brisket = $_POST['brisket']; $carrots = $_POST['carrots']; if(isset($_POST['record_id']) && !empty($_POST['record_id'])){ $record_id=$_POST['record_id']; foreach($_POST as $indexName => $value){ if ($indexName!="record_id" && $indexName!="action"){ $sql=("UPDATE testtable SET $indexName='$value' WHERE id=$record_id"); $result=mysql_query($sql); }//if ($indexName!="record_id" && $indexName!="action") }//foreach($_POST as $indexName => $value) }//if(isset($_POST['record_id']) && !empty($_POST['record_id'])) }//if(isset($_POST['action']) && $_POST['action']=="submitform") ?> Quote Link to comment https://forums.phpfreaks.com/topic/257885-any-easy-way-to-list-update-columns/#findComment-1321928 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.