Jump to content

Arrays, Loops and Queries


radar

Recommended Posts

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 ) = Array

Can anyone see why this would do this?  Perhaps I am just trying to do it totally wrong...
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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...
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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...
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.