Jump to content

Recommended Posts

Hey guys, I am having trouble with a dynamic form.  This will allow a customer to edit fields in a database (no matter how many are in the database).  Here is my form:

<?php
include '../../header.php';

$query = "select * from meals where active='1'";
$meals = array();
$result=mysql_query($query);
while (($row = mysql_fetch_assoc($result)) !== false) {
  $meals[] = $row;
}
?>
<form action="editM2.php" method="POST">
<table>
<?php 
	foreach ($meals as $m) {
?><tr>
   	<td valign="top">Name:</td>
<td valign="top"><input type="text" name="<?php echo $m['name']; ?>" value="<?php echo $m['name']; ?>" /></td>
<td valign="top">Description: </td>
<td valign="top"><textarea name="d<?php echo $m['name']; ?>"><?php echo $m['description']; ?></textarea></td>
  </tr>
<?php
}
?>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
<?php
include '../../footer.php';
?>

 

Now here is my big problem....how I do I process these results?  Here is what I was attempting, but it doesn't work.  I don't know how to get the information from the form.

 

<?php
include '../system.php';

$query = "select * from meals where active='1'";
$meals = array();
$result=mysql_query($query);
while (($row = mysql_fetch_assoc($result)) !== false) {
  $meals[] = $row;
}

  foreach ($meals as $m) {
$$m['name'] = $_POST['$m['name']'];
$d$m['name'] = $_POST['d$m['name']'];
  }

?>

Link to comment
https://forums.phpfreaks.com/topic/85256-solved-form-help-looping/
Share on other sites

Change the HTML code to this:

<input type="text" name="name[<?php echo $m['name']; ?>]" value="<?php echo $m['name']; ?>" />
and
<textarea name="desc[<?php echo $m['name']; ?>]"><?php echo $m['description']; ?></textarea>

 

And then loop through:

<?php
foreach($_POST['name'] as $n => $v) {
  mysql_query("UPDATE database SET name='". $v ."', description='". $_POST['desc'][$n] ."' WHERE name = '". $v ."'");
}
?>

 

DO NOT use this code without validating the user input. And you should create one query to update instesd of one for every entry.

Your code seems slightly bizarre - if you are allowing the users to edit the name of the meal, then how can you expect to retrieve this from the form using the old name stored in the database?

Yeah it is bizarre.  The basic idea is that the user can change the name and description of the meals in the database.  I know there is probably a better way to do this, I just can't think of it.  (I'm sure it's something simple too)

Change the HTML code to this:

<input type="text" name="name[<?php echo $m['name']; ?>]" value="<?php echo $m['name']; ?>" />
and
<textarea name="desc[<?php echo $m['name']; ?>]"><?php echo $m['description']; ?></textarea>

 

And then loop through:

<?php
foreach($_POST['name'] as $n => $v) {
  mysql_query("UPDATE database SET name='". $v ."', description='". $_POST['desc'][$n] ."' WHERE name = '". $v ."'");
}
?>

DO NOT use this code without validating the user input. And you should create one query to update instesd of one for every entry.

 

I tried this and although it doesn't give me any errors.  It doesn't seem to be updating the database at all.  (I switched 'database' to the actual name of the database). 

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.