radar Posted September 3, 2006 Share Posted September 3, 2006 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...[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);?>[/code]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 ) = ArrayCan anyone see why this would do this? Perhaps I am just trying to do it totally wrong... Quote Link to comment https://forums.phpfreaks.com/topic/19596-arrays-loops-and-queries/ Share on other sites More sharing options...
wildteen88 Posted September 3, 2006 Share Posted September 3, 2006 Is the fields in your form the same name as whats in the config_name column if they are use a foreach loop:[code=php:0]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);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19596-arrays-loops-and-queries/#findComment-85258 Share on other sites More sharing options...
radar Posted September 3, 2006 Author Share Posted September 3, 2006 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_namedomain_namesite_namesite_descripsite_disableuser_activationvisual_confand 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... Quote Link to comment https://forums.phpfreaks.com/topic/19596-arrays-loops-and-queries/#findComment-85262 Share on other sites More sharing options...
wildteen88 Posted September 3, 2006 Share Posted September 3, 2006 Then mny code above should work for you. Did you try it out?[quote author=wildteen88 link=topic=106755.msg427228#msg427228 date=1157306905][code=php:0]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);[/code][/quote] Quote Link to comment https://forums.phpfreaks.com/topic/19596-arrays-loops-and-queries/#findComment-85263 Share on other sites More sharing options...
radar Posted September 3, 2006 Author Share Posted September 3, 2006 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..[code]<?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);}?>[/code]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... Quote Link to comment https://forums.phpfreaks.com/topic/19596-arrays-loops-and-queries/#findComment-85265 Share on other sites More sharing options...
wildteen88 Posted September 3, 2006 Share Posted September 3, 2006 By the way, $datas and $names variables are created by the foreach loop automatically.. So this code:[code]$names = array(); $data = array(); $names = mysql_fetch_array(mysql_query("SELECT config_name FROM config")); $data = $_POST;[/code]Isn't needed. Just [code=php:0]foreach($_POST as $datas => $names)[/code] will do.Also you have the $datas and $names variables round the wrong way in the foreach loop it shold be this:[code=php:0]foreach($_POST as $names => $datas)[/code]$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:[code=php:0]foreach($data as $names => $datas){ $mysite = "UPDATE `config` SET `config_value` = ".$datas." WHERE CONVERT( `config_name` USING utf8 ) = ". $names .""; $mysite = mysql_query($mysite);}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19596-arrays-loops-and-queries/#findComment-85274 Share on other sites More sharing options...
radar Posted September 3, 2006 Author Share Posted September 3, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/19596-arrays-loops-and-queries/#findComment-85284 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.