robert_a89 Posted July 11, 2010 Share Posted July 11, 2010 Hello, I am attempting to create a form which would allow update of fields in a database. Existing data stored in the database is used to populate the default values, and I want it so that they can change the fields and press a button to update the database. A differently named form appears on the page for each row in the database. The code uses a mixture of HTML and PHP. However, when using the update button it does not update the database. Any help would be muchly appreciated <?php $db = new mysqli ('*****','*****,'*****','*****');//connect to db $results = $db->query("select * from images"); while ($row = $results->fetch_assoc()){?> <form name="form[<?php echo $row['pkey']?>]" method="post" enctype="multipart/form-data" action=""> <h2><?php print $row['location']?></h2> <table width="456"> <tr> <td>Title: </td> <td><input type="text" name="Title[<?php echo $row['pkey']?>]" value="<?php echo $row['title']?>" ></td> </tr> <tr> <td>Type: </td> <td><select name="type_sel[<?php echo $row['pkey']?>]"> <option><?php echo $row['type'];?></option> <?php $results2 = $db->query("select type from work_types"); while ($row2 = $results2->fetch_assoc()){?> <option><?php echo $row2['type']?></option> <?php }?> </select></td> </tr> <tr> <td>Collection: </td> <td><select name="coll_sel[<?php echo $row['pkey']?>]"> <option><?php echo$row['type'];?></option> <?php $results3 = $db->query("select distinct coll_name from coll_name"); while ($row3 = $results3->fetch_assoc()){?> <option><?php echo $row3['coll_name']?></option> <?php }?> </select></td> </tr> <tr> <td>Date: </td> <td><input type="text" name="date[<?php echo $row['pkey']?>]" /></td> </tr> <tr> <td>Description: </td> <td><textarea cols=80 rows=5 name="desc[<?php echo $row['pkey']?>]"></textarea></td> </tr> <tr> <td><input name="Submit[<?php echo $row['pkey']?>]" type="submit" value="Upload image"></td> </tr> </table> <?php if(isset($_POST['Submit["'.$row['pkey'].'"]'])) { $pkey = $row['pkey']; print $pkey; $title=$_POST['Title["'.$row['pkey'].'"]']; $type_sel=$_POST['type_sel["'.$row['pkey'].'"]']; $coll_sel=$_POST['coll_sel["'.$row['pkey'].'"]']; $date=$_POST['date["'.$row['pkey'].'"]']; $desc=$_POST['desc["'.$row['pkey'].'"]']; $results4 = $db->query("UPDATE images SET title='$title' WHERE pkey='$pkey'"); } ?> </form> <?php } $db->close(); ?> Thank you in advance for your help Quote Link to comment Share on other sites More sharing options...
khr2003 Posted July 13, 2010 Share Posted July 13, 2010 for a start try to place the post block outside the while loop. why the names of the input is followed by <?php echo $row['pkey']?> ? if you want unique input values try the "id" attribute. Quote Link to comment Share on other sites More sharing options...
robert_a89 Posted July 13, 2010 Author Share Posted July 13, 2010 The names of teh inputs are followed by that so that each instance of it appearing on the page has it's own name. I've found out that this is causing the issue with updating it and so cut it out. Taking the POST block out of the while loop causes it to update only the last row of the database, whilst leaving it as it is updates all the field to the contents of the box where the button is pressed. Heres a simplified code, using just one of the variables to update in the database <?php $db = new mysqli ('localhost','*****','*****','*****');//connect to db $results = $db->query("select * from images"); while ($row = $results->fetch_assoc()){ $counter=$row['pkey'];?> <form name="newad" method="post" enctype="multipart/form-data" action=""> <table width="456"> <tr> <td>Title: </td> <td><input type="text" name="Title" value="<?php echo $row['title']?>" ></td> <td><input type="submit" name="Submit" /></td></tr> </table> </form> <?php } if(isset($_POST['Submit'])) { $title=$_POST['Title']; $results4 = $db->query("UPDATE images SET title='$title' WHERE pkey='$counter'"); } ?> <?php //} $db->close(); ?> Quote Link to comment 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.