Janus13 Posted April 23, 2006 Share Posted April 23, 2006 I'm trying to update a sql table with a php script, and I'm trying to figure out if there is an easier way to do it than the way I am currently doing it.I have a 2 column table that stores configuration data, the tables are config and config_settings. There are 10 rows in those 2 columns. I have a page where I allow editing of those settings, then I submit them into the database. The way I'm currently doing it is like so:[code]$query = "UPDATE config SET config_setting = '$value' where config = '$colname'";$db->dbQuery($query); //This is a db class I use$query = "UPDATE config SET config_setting = '$value' where config = '$colname2'";$db->dbQuery($query);[/code]and so on and so forth. I was thinking I might be able to read my settings posted from the previous page via $_POST variables into an array then do a foreach through the array and submit to the database like that, but I can't figure out how, or if it's possible.Am I ok doing what I am doing now, or is there a cleaner way of doing it?Thanks for your help! Quote Link to comment Share on other sites More sharing options...
Janus13 Posted April 23, 2006 Author Share Posted April 23, 2006 I found a little better way to do it so it's not as much code[code]$query = array();$query[0] = "first query";$query[1] = "second query";//etcforeach($query as $key=>$value): mysql_query("$value") or die(mysql_error());endforeach;print "Changes submitted";[/code]But for each row in that table I have to manually define an array value, so what I'm trying to figure out now is can you read the contents of the $_POST array from a form into an array somehow?Well I figured that out.. and it was easier than I thought.. I just decided to see if it was possible to dump the entire $_POST array, and it was. Simply doing a foreach loop like so:[code]foreach($_POST as $key=>$value): print "$value<BR>";endforeach;[/code]But that also displayed the name of the submit button, so I was scratching my head wondering how on earth to supress the button name, then I was like.. It can't be that easy.. but it was, I just removed the name tag from the button and bingo! Just what I needed. So I guess I was just sharing in the end. Thanks anyway!EDIT: Well maybe I spoke to soon. I can get the config_setting value from the array, but which row to set it to presents me a new problem.... Quote Link to comment Share on other sites More sharing options...
mb81 Posted April 23, 2006 Share Posted April 23, 2006 [!--quoteo(post=367581:date=Apr 22 2006, 09:27 PM:name=Janus13)--][div class=\'quotetop\']QUOTE(Janus13 @ Apr 22 2006, 09:27 PM) [snapback]367581[/snapback][/div][div class=\'quotemain\'][!--quotec--]Well I figured that out.. and it was easier than I thought.. I just decided to see if it was possible to dump the entire $_POST array, and it was. Simply doing a foreach loop like so:[code]foreach($_POST as $key=>$value): print "$value<BR>";endforeach;[/code]EDIT: Well maybe I spoke to soon. I can get the config_setting value from the array, but which row to set it to presents me a new problem....[/quote]The $key value is the name of the input, so you would just do it like this:[code]foreach ($_POST AS $key=>$value) { $db->dbQuery("UPDATE config SET config_setting = '$value' where config = '$key'");}[/code] Quote Link to comment Share on other sites More sharing options...
Janus13 Posted April 23, 2006 Author Share Posted April 23, 2006 [!--quoteo(post=367586:date=Apr 22 2006, 09:58 PM:name=mb81)--][div class=\'quotetop\']QUOTE(mb81 @ Apr 22 2006, 09:58 PM) [snapback]367586[/snapback][/div][div class=\'quotemain\'][!--quotec--]The $key value is the name of the input, so you would just do it like this:[code]foreach ($_POST AS $key=>$value) { $db->dbQuery("UPDATE config SET config_setting = '$value' where config = '$key'");}[/code][/quote]Ah, that makes sense. Whatever I set the name setting of the input/selects to is what carries over as the key so it works out that I normally set my name key as the setting name. That ended up being as easy as I originally thought. Thanks for the help! Quote Link to comment Share on other sites More sharing options...
mb81 Posted April 23, 2006 Share Posted April 23, 2006 [!--quoteo(post=367596:date=Apr 22 2006, 10:23 PM:name=Janus13)--][div class=\'quotetop\']QUOTE(Janus13 @ Apr 22 2006, 10:23 PM) [snapback]367596[/snapback][/div][div class=\'quotemain\'][!--quotec--]Ah, that makes sense. Whatever I set the name setting of the input/selects to is what carries over as the key so it works out that I normally set my name key as the setting name. That ended up being as easy as I originally thought. Thanks for the help![/quote]I actually loop through the same way to actually print out each item on the configuration page, that way I can add a configuration setting by adding a row in the MySQL database, I use just one more column, which is a type variable. just to get your mind thinking if you are building larger scale applications, it makes it easier to update. Quote Link to comment 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.