Jump to content


Photo

Easiest way to update table


  • Please log in to reply
4 replies to this topic

#1 Janus13

Janus13
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 23 April 2006 - 01:59 AM

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:

$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);

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!

#2 Janus13

Janus13
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 23 April 2006 - 02:27 AM

I found a little better way to do it so it's not as much code

$query = array();
$query[0] = "first query";
$query[1] = "second query";
//etc
foreach($query as $key=>$value):           
    mysql_query("$value") or die(mysql_error());
endforeach;
print "Changes submitted";

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:

foreach($_POST as $key=>$value):
   print "$value<BR>";
endforeach;

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....

#3 mb81

mb81
  • Members
  • PipPipPip
  • Advanced Member
  • 120 posts

Posted 23 April 2006 - 02:58 AM

[!--quoteo(post=367581:date=Apr 22 2006, 09:27 PM:name=Janus13)--][div class=\'quotetop\']QUOTE(Janus13 @ Apr 22 2006, 09:27 PM) View Post[/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:

foreach($_POST as $key=>$value):
   print "$value<BR>";
endforeach;

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:
foreach ($_POST AS $key=>$value) {
     $db->dbQuery("UPDATE config SET config_setting = '$value' where config = '$key'");
}



#4 Janus13

Janus13
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 23 April 2006 - 03:23 AM

[!--quoteo(post=367586:date=Apr 22 2006, 09:58 PM:name=mb81)--][div class=\'quotetop\']QUOTE(mb81 @ Apr 22 2006, 09:58 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
The $key value is the name of the input, so you would just do it like this:
foreach ($_POST AS $key=>$value) {
     $db->dbQuery("UPDATE config SET config_setting = '$value' where config = '$key'");
}
[/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!

#5 mb81

mb81
  • Members
  • PipPipPip
  • Advanced Member
  • 120 posts

Posted 23 April 2006 - 03:49 AM

[!--quoteo(post=367596:date=Apr 22 2006, 10:23 PM:name=Janus13)--][div class=\'quotetop\']QUOTE(Janus13 @ Apr 22 2006, 10:23 PM) View Post[/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.








0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users