Jump to content

An array generated by a form to update mysql database.


samtwilliams

Recommended Posts

Hi all,

 

I am stuck and a little lost ;) trying to use a form to post multiple values to update a mysql database. I have created a form which is generated from a list of rows out of the database which allows a user to update a certain column, i am just stuck with how i actually peform the update with the array.

 

My Form code is below;

<form name="settings" method="post" action="exec.php">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?PHP while ($row = mysql_fetch_assoc($result)){ ?>
  <tr>
    <td><?PHP echo $row['name']; ?></td>
    <td><input name="<?PHP echo $row['name']; ?>" type="text" id="<?PHP echo $row['name']; ?>" value="<?PHP echo $row['value']; ?>" size="100">
    
    </td>
    <td><?PHP echo $row['help']; ?></td>
  </tr>
  <?PHP } ?>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>
<input type="submit" name="Submit" id="Submit" value="Submit">
  </form>

 

I have checked what the array looks like and it looks like this part is working fine, the array looks like;

 

Array
(
    [global_title_index] => a
    [global_logo_title] => b
    [global_logo_text] => c
    [global_webmaster_link] => d
    [global_footer_webinfotop] => e
    [submit] => Submit
)

 

So all i am trying to achieve is to insert this data into my database;

 

                                  -------------------------------

column names:                    name            |      value

                                  global_title_index  |          a

                                  global_title_title    |          b

 

Hope someone understands what i am trying to achieve, and hope they can help :)

 

Sam

I have been playign with this but not sure if i am on the right track? Any ideas anyone?

 

function mysql_update_array($table, $data, $id_field, $id_value) {
foreach ($data as $field=>$value) {
	$fields[] = sprintf("`%s` = '%s'", $field, mysql_real_escape_string($value));
}
$field_list = join(',', $fields);

$query = sprintf("UPDATE `%s` SET %s WHERE `%s` = %s", $table, $field_list, $id_field, intval($id_value));

return $query;
}

mysql_update_array(globals, $_POST, name, $_POST);

Archived

This topic is now archived and is closed to further replies.

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