dfowler Posted January 9, 2008 Share Posted January 9, 2008 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']']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85256-solved-form-help-looping/ Share on other sites More sharing options...
GingerRobot Posted January 9, 2008 Share Posted January 9, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/85256-solved-form-help-looping/#findComment-434968 Share on other sites More sharing options...
Norsk.Firefox Posted January 9, 2008 Share Posted January 9, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85256-solved-form-help-looping/#findComment-434978 Share on other sites More sharing options...
dfowler Posted January 10, 2008 Author Share Posted January 10, 2008 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) Quote Link to comment https://forums.phpfreaks.com/topic/85256-solved-form-help-looping/#findComment-435593 Share on other sites More sharing options...
dfowler Posted January 10, 2008 Author Share Posted January 10, 2008 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). Quote Link to comment https://forums.phpfreaks.com/topic/85256-solved-form-help-looping/#findComment-435595 Share on other sites More sharing options...
dfowler Posted January 10, 2008 Author Share Posted January 10, 2008 Ok I figured it out, the loop does pull the names correctly. However, I have to change the WHERE value in order for it to work. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/85256-solved-form-help-looping/#findComment-435603 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.