MjM8082 Posted August 17, 2011 Share Posted August 17, 2011 Still trying to get this UPDATE statement to work... My delete statement works fine... But I think the problem with my update statement is that it is not reading the information from the form and being put into the database. I've tried everything I can think of, and still can't figure out why this is not working. Need help!! here is my code... <html> <head> <title>Update User</title> </head> <body> <form method="post" action="update_user2.php"> <?php $dbc = mysqli_connect('localhost', 'se266_user', 'pwd', 'se266') or die(mysql_error()); //delete users echo '<b>Delete or Update User</b>.<br />'; if (isset($_POST['remove'])) { foreach($_POST['delete'] as $delete_id) { $query = "DELETE FROM users WHERE course_id = $delete_id"; mysqli_query($dbc, $query) or die ('can\'t delete course'); } echo 'user has been deleted.<br />'; } if (isset($_POST['update'])) { foreach($_POST['update'] as $update_id) { $course_id = $_POST['course_id']; $course_name = $_POST['course_name']; $student_id = $_POST['student_id']; $query = "UPDATE users SET course_name ='". $course_name ."' WHERE course_id = $course_id"; mysqli_query($dbc, $query) or die ('can\'t update course'); $update_count = $db->exec($query); } echo 'course has been updated.<br />'; } //display users info with checkbox to delete $query = "SELECT * FROM users"; $result = mysqli_query($dbc, $query); while($row = mysqli_fetch_array($result)) { echo '<input type="checkbox" value="' .$row['course_id'] . '" name="delete[]" />'; echo ' ' .$row['course_name'] .' '. $row['student_id']; echo '<br />'; } mysqli_close($dbc); ?> <input type="submit" name="remove" value="Remove" /> <input type="submit" name="update" value="Update" /> <br> <br> <form method="POST" action="update_user2.php"> <label for="course_id">Course ID:</label> <input type="text" id="course_id" name="course_id" /><br /> <label for="course_name">Course Name:</label> <input type="text" id="course_name" name="course_name" /><br /> <label for="course_name">Student ID:</label> <input type="text" id="student_id" name="student_id" /><br /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/244971-how-to-connect-my-update-statement-to-my-form/ Share on other sites More sharing options...
skwap Posted August 17, 2011 Share Posted August 17, 2011 Still trying to get this UPDATE statement to work... My delete statement works fine... But I think the problem with my update statement is that it is not reading the information from the form and being put into the database. I've tried everything I can think of, and still can't figure out why this is not working. Need help!! here is my code... <html> <head> <title>Update User</title> </head> <body> <form method="post" action="update_user2.php"> <?php $dbc = mysqli_connect('localhost', 'se266_user', 'pwd', 'se266') or die(mysql_error()); //delete users echo '<b>Delete or Update User</b>.<br />'; if (isset($_POST['remove'])) { foreach($_POST['delete'] as $delete_id) { $query = "DELETE FROM users WHERE course_id = $delete_id"; mysqli_query($dbc, $query) or die ('can\'t delete course'); } echo 'user has been deleted.<br />'; } if (isset($_POST['update'])) { foreach($_POST['update'] as $update_id) { $course_id = $_POST['course_id']; $course_name = $_POST['course_name']; $student_id = $_POST['student_id']; $query = "UPDATE users SET course_name ='". $course_name ."' WHERE course_id = $course_id"; mysqli_query($dbc, $query) or die ('can\'t update course'); $update_count = $db->exec($query); } echo 'course has been updated.<br />'; } //display users info with checkbox to delete $query = "SELECT * FROM users"; $result = mysqli_query($dbc, $query); while($row = mysqli_fetch_array($result)) { echo '<input type="checkbox" value="' .$row['course_id'] . '" name="delete[]" />'; echo ' ' .$row['course_name'] .' '. $row['student_id']; echo '<br />'; } mysqli_close($dbc); ?> <input type="submit" name="remove" value="Remove" /> <input type="submit" name="update" value="Update" /> <br> <br> <form method="POST" action="update_user2.php"> <label for="course_id">Course ID:</label> <input type="text" id="course_id" name="course_id" /><br /> <label for="course_name">Course Name:</label> <input type="text" id="course_name" name="course_name" /><br /> <label for="course_name">Student ID:</label> <input type="text" id="student_id" name="student_id" /><br /> </form> </body> </html> just change your UPDATE statement to $query = "UPDATE `users` SET `course_name` = '$course_name' WHERE `course_id` = '$course_id'"; Quote Link to comment https://forums.phpfreaks.com/topic/244971-how-to-connect-my-update-statement-to-my-form/#findComment-1258380 Share on other sites More sharing options...
MjM8082 Posted August 17, 2011 Author Share Posted August 17, 2011 This still didn't fix the problem. I don't see why it's not fixing the problem. Everything seems to be right.. Quote Link to comment https://forums.phpfreaks.com/topic/244971-how-to-connect-my-update-statement-to-my-form/#findComment-1258414 Share on other sites More sharing options...
emvy03 Posted August 17, 2011 Share Posted August 17, 2011 Hi, Not sure if its any help but I think I had a similar problem and I inserted a hidden field into the form containing the ID. Quote Link to comment https://forums.phpfreaks.com/topic/244971-how-to-connect-my-update-statement-to-my-form/#findComment-1258477 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.