EternalSorrow Posted July 14, 2009 Share Posted July 14, 2009 I'm currently working on creating a form which will allow me to edit rows in my mysql database, but I've hit a large snag. I have only two test rows in the database and am using two fields called 'title' and 'author'. Here are the problems: [*]When I submit the edit form to the database to the first row, it does not change. [*]However, the second row information will change to the first row information (as if I had inputted the information into the second row all along). I'd be grateful for any help on this matter, as I'm stumped as to the problem. Here's the full code for the edit, and here's a link to a 'working' example. <? mysql_connect(localhost,user,pw); @mysql_select_db(db) or die( "Unable to select database"); if(!isset($cmd)) { $result = mysql_query("SELECT * FROM `book`"); while ($row = mysql_fetch_array($result)) { extract($row); echo "<a href='edit.php?cmd=edit&id=$id'><b>$title</b> by $author</a>"; echo "<p>"; } } ?> <? if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") { if (!isset($_POST["submit"])) { $id = $_GET["id"]; $sql = "SELECT * FROM book WHERE id=$id"; $result = mysql_query($sql); $myrow = mysql_fetch_array($result); ?> <form action="edit.php" method="post"> <input type=hidden name="id" value="<?php echo $myrow["id"] ?>"> Title:<INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["title"] ?>"><br> Author:<INPUT TYPE="TEXT" NAME="author" VALUE="<?php echo $myrow["author"] ?>"><br> <input type="hidden" name="cmd" value="edit"> <input type="submit" name="submit" value="submit"> </form> <? } ?> <? if ($_POST["submit"]) { $title = $_POST["title"]; $who = $_POST["author"]; $sql = "UPDATE book SET title='$title',author='$author' WHERE id=$id" or die(mysql_error()); $result = mysql_query($sql); echo "Thank you! Information updated."; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/165891-solved-update-a-row/ Share on other sites More sharing options...
sasa Posted July 14, 2009 Share Posted July 14, 2009 change <? if ($_POST["submit"]) { $title = $_POST["title"]; $who = $_POST["author"]; $sql = "UPDATE book SET title='$title',author='$author' WHERE id=$id" or die(mysql_error()); $result = mysql_query($sql); echo "Thank you! Information updated."; } } ?> tochange <? if ($_POST["submit"]) { $title = $_POST["title"]; $who = $_POST["author"]; $id = $_POST['id']; $sql = "UPDATE book SET title='$title',author='$who' WHERE id=$id"; $result = mysql_query($sql) or die(mysql_error()); echo "Thank you! Information updated."; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/165891-solved-update-a-row/#findComment-875029 Share on other sites More sharing options...
EternalSorrow Posted July 14, 2009 Author Share Posted July 14, 2009 Thanks so much, the code worked perfectly. I knew the problem lay in the id field not properly working in the final code snippet, but was unable to find why it would not work. Case solved, thank God. Quote Link to comment https://forums.phpfreaks.com/topic/165891-solved-update-a-row/#findComment-875136 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.