Jump to content


Photo

Arrays, Loops and Queries


  • Please log in to reply
6 replies to this topic

#1 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 03 September 2006 - 05:59 PM

Okay so I have a rather large form for my config which is set up the way I like it.. though I need to do the queries to add them all into the database.. now with 50+ form fields I'm sure you can see why I am not inclined to do each query seperately by typing each query..  So I've come up with a little code, though it doesnt work as I had hoped....  Now I've altered it a little bit so that I could see the output of the file...  So here is the code...
<?php
$names = array();
$data = array();
names = mysql_fetch_assoc(mysql_query("SELECT config_name FROM config"));
$data = $_POST;
for ($i = 0; $i < sizeof($data); $i++) {
$mysite .= "UPDATE `config` SET `config_value` = ".each($data)." WHERE CONVERT( `config_name` USING utf8 ) = ".each($names)."<br>";
$tpl->assign('errmsg', $mysite);
?>

note that this is not the actual beginning of the file -- put php tags for syntax highlighting...  anyway that is my code... and this is what it is spitting out..  it spits this out over and over until all the form fields are gone...

UPDATE `config` SET `config_value` = Array WHERE CONVERT( `config_name` USING utf8 ) = Array

Can anyone see why this would do this?  Perhaps I am just trying to do it totally wrong...

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 September 2006 - 06:08 PM

Is the fields in your form the same name as whats in the config_name column if they are use a foreach loop:
foreach($_POST as $field_name => $field_value)
{
    $mysite .= "UPDATE `config` SET `config_value` = ". $field_value ." WHERE CONVERT( `config_name` USING utf8 ) = ". $field_name ."<br />\n";
}
$tpl->assign('errmsg', $mysite);


#3 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 03 September 2006 - 06:13 PM

in my config table of my database i have to colums.. one is config_name the other is config_value.. here is the basics of config_name

domain_name
site_name
site_descrip
site_disable
user_activation
visual_conf

and it goes on from there..  I have the form fields set up on the page in the exact same listing as they are in the database..  So row 1 is the domain name, row 2 is the site name, etc...

Now the code you gave me seems to work.. as in the output is what it needs to be..  though when I run the query it doesn't seem to change it...

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 September 2006 - 06:18 PM

Then mny code above should work for you. Did you try it out?

foreach($_POST as $field_name => $field_value)
{
    $mysite .= "UPDATE `config` SET `config_value` = ". $field_value ." WHERE CONVERT( `config_name` USING utf8 ) = ". $field_name ."<br />\n";
}
$tpl->assign('errmsg', $mysite);



#5 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 03 September 2006 - 06:25 PM

I did try it out.. It seems to work now as in the output has the correct names and stuff..  though when I try and run it, it doesn't update the things in the database..  here is how I have it set as of now..

<?php
					$names = array();
					$data = array();
					$names = mysql_fetch_array(mysql_query("SELECT config_name FROM config"));
					$data = $_POST;
					foreach($data as $datas => $names)
{
    $mysite = "UPDATE `config` SET `config_value` = ".$datas." WHERE CONVERT( `config_name` USING utf8 ) = ". $names ."";
	$mysite = mysql_query($mysite);
}
?>

edit: well i feel like an idiot i re-looked at the output and they are just backwards lol.. let me try that..

so I got them in the right order but it still doesnt update in the database...

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 September 2006 - 06:48 PM

By the way, $datas and $names variables are created by the foreach loop automatically.. So this code:
$names = array();
					$data = array();
					$names = mysql_fetch_array(mysql_query("SELECT config_name FROM config"));
					$data = $_POST;
Isn't needed. Just
foreach($_POST as $datas => $names)
will do.

Also you have the $datas and $names variables round the wrong way in the foreach loop it shold be this:
foreach($_POST as $names => $datas)
$names holds the key in the $_POST arrray, ($_POST['domain_name'] - domain_name is the key in the $_POST array) and $datas holds the value form the form field.

So just use this as the code:
foreach($data as $names => $datas)
{
    $mysite = "UPDATE `config` SET `config_value` = ".$datas." WHERE CONVERT( `config_name` USING utf8 ) = ". $names ."";
    $mysite = mysql_query($mysite);
}


#7 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 03 September 2006 - 07:10 PM

Sweet looks like it's working..  I had to go through my form and make sure all the form fields were named the same as the config_name in the database to make sure that would work.. and then I had to edit my error checking and edit a couple custom functions i wrote for my radial buttons.. but it saved my domain name, site name and site descrip so it seems as though it works...

Thank you for the help...

// Justin aka Radar




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users