MattD00 Posted February 15, 2011 Share Posted February 15, 2011 I created a form that would display all the information that was in my database within a textbox using a while loop. The infomation is able to be displayed but when trying to update only the information in the last textbox gets updated and the other textboxes will just stay the same. <?php include ('connection.php'); ?> <form method="post" action="update.php"> <?php $display = mysql_query("SELECT * FROM book ORDER BY id asc") or die("Table Error"); $ID = $_POST['id']; $Title = $_POST['title']; $Cost = $_POST['cost']; $Stock = $_POST['stock']; while ($row = mysql_fetch_assoc($display)) { echo "ID :<input type=\"text\" name=\"".id."\" value = \"". $row['id'] ."\"/readonly>"; echo "Title :<input type=\"text\" name=\"".title."\" value = \"" . $row['title'] . "\"/>"; echo "Cost :<input type=\"text\" name=\"".cost."\" value = \"" .$row['cost'] . "\"/>"; echo "Stock :<input type=\"text\" name=\"".stock."\" value = \"" . $row['stock'] . "\"/>"; mysql_query("UPDATE book SET title = '" .$Title. "', cost = '" .$Cost. "', stock = '" .$Stock. "' WHERE id = '" .$ID. "'"); } ?> <input type="submit" name="update" value="Update" /> Im not sure why it is doing this if someone could tell me it would be a big help Thanks Quote Link to comment https://forums.phpfreaks.com/topic/227761-php-update-with-while-loop/ Share on other sites More sharing options...
BlueSkyIS Posted February 15, 2011 Share Posted February 15, 2011 you should perform updates at the top of the code before any output is displayed. don't make updates while looping over data for output. Quote Link to comment https://forums.phpfreaks.com/topic/227761-php-update-with-while-loop/#findComment-1174572 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.