MjM8082 Posted August 16, 2011 Share Posted August 16, 2011 I am not seeing what I'm doing wrong. When I click on a check box for the Course class I want to delete.. it just says can't delete. I had this working before, but now its not working. here is the update_user page with the DELETE statement on it... <html> <head> <title>Update User</title> </head> <body> <form method="post" action="update_user.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 user'); } echo 'user has been deleted.<br />'; } else if (isset($_POST['update'])) { foreach($_POST['update'] as $update_id) { $query = "UPDATE FROM users WHERE user_id = $update_id"; mysqli_query($dbc, $query) or die ('can\'t update user'); $course_id = $_POST['course_id']; $course_name = $_POST['course_name']; $student_id = $_POST['student_id']; $sql = "INSERT INTO users (course_id, course_name, student_id) VALUES ('$course_id', '$course_name', '$student_id');"; // added missing single quotes $db->exec($sql); } echo 'user 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> And this is the form page if needed... <?php error_reporting(E_ALL); // add maximum error reporting for debugging ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <!-- the head section --> <head> <title>Lab 3</title> </head> <!-- the body section --> <body> <h1> <i> Add Course </i></h1> <?php require_once('database.php'); if (isset($_POST['btn_add'])) { $course_id = $_POST['course_id']; $course_name = $_POST['course_name']; $student_id = $_POST['student_id']; $sql = "INSERT INTO users (course_id, course_name, student_id) VALUES ('$course_id', '$course_name', '$student_id');"; // added missing single quotes $db->exec($sql); } $query = "SELECT * FROM users"; $users = $db->query($query); // display each of these users echo "<ul>"; foreach ($users as $u) { $course = $u['course_id']; $name = $u['course_name']; $id = $u['student_id']; echo "<li><a href='update_user2.php?id=$id'>$course $name</a></li>"; } echo "</ul>"; ?> <hr /> <form method="POST" action="lab3.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 /> <input type="submit" value="Add Course" name="btn_add" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/244943-cant-get-my-delete-statement-to-work/ Share on other sites More sharing options...
PFMaBiSmAd Posted August 16, 2011 Share Posted August 16, 2011 Your die() statement (which is only useful for troubleshooting purposes BTW) needs to tell you why the query failed and what the query is. Try this - mysqli_query($dbc, $query) or die ("Can't delete user. Query: $query, Error: " . mysqli_error($dbc)); You are actually kind of lucky that it is not deleting anything because the query you are using will delete all the students with a specific course_id (assuming that the course_id column is not an auto-increment id for that table), not just the row with the student_id that you picked. Quote Link to comment https://forums.phpfreaks.com/topic/244943-cant-get-my-delete-statement-to-work/#findComment-1258212 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.