pctn513 Posted February 15, 2014 Share Posted February 15, 2014 Hey everyone, I have a database that displays a list of users that have been added to a 'Favorites List'. The user has the ability to remove/delete users from the list using checkboxes and a delete button; however, when the user clicks the delete button after selecting the user they wish to delete, the page will refresh but the user still remains in the database instead of being delete. Can someone help me out? I have supplied my current code. <?php /// Connect to database. /// include_once("scripts/connect_to_mysql.php"); include_once("scripts/functions.php"); /// Start secure session. /// sec_session_start(); if (isset($_GET['id'])) { $id = preg_replace('#[^0-9]#i', '', $_GET['id']); } else if (isset($_SESSION['id'])) { $id = $_SESSION['id']; } else { header("location: logout.php"); exit(); } $id = preg_replace('#[^0-9]#i', '', $id); /// Grab information of user logged-in $displayList = ''; $sql_user_data = mysql_query("SELECT id FROM members WHERE id='$_SESSION[id]' LIMIT 1") or die (mysql_error()); while($row = mysql_fetch_array($sql_user_data)) { $userid = $row["id"]; } $sql_favorites_match = mysql_query("SELECT id, member_id, favorite_id FROM favorites WHERE member_id='$userid'") or die (mysql_error()); while($row = mysql_fetch_array($sql_favorites_match)) { $favid = $row["id"]; $member_id = $row["member_id"]; $favorite_id = $row["favorite_id"]; $displayList .='<div>Favorite User ID: '.$favorite_id.' <input id="checkbox[]" name="checkbox[]" type="checkbox" value='.$favid.'></div>'; } if(isset($_POST['favdelete'])) { $checkbox = $_POST['checkbox']; //from name="checkbox[]" $countCheck = count($_POST['checkbox']); for($i=0;$i<$countCheck;$i++) { $del_id = $checkbox[$i]; $sql = "DELETE from favorites where id = $del_id"; $result = $mysqli->query($sql) or die(mysqli_error($mysqli)); } header("Location: ./deletechk_test.php"); } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted February 15, 2014 Share Posted February 15, 2014 You are trying to use a mix of mysql and mysqli. You have to use one or the other, preferably mysqli Quote Link to comment Share on other sites More sharing options...
pctn513 Posted February 15, 2014 Author Share Posted February 15, 2014 I've switched to just mysql for now. This still does not resolve the issue. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 15, 2014 Share Posted February 15, 2014 Where does $_POST['favdelete'] get sent from? And I do not see a form anywhere. Quote Link to comment Share on other sites More sharing options...
pctn513 Posted February 15, 2014 Author Share Posted February 15, 2014 Sorry, I didnt include my html code. Here it is: <!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Checkbox delete test</title> </head> <body> <?php print "$displayList"; ?> <br> <form action="deletechk_test.php" method="post" name="myForm"> <input name="favdelete" type="submit" value="Delete" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Barand Posted February 15, 2014 Share Posted February 15, 2014 The checkboxes are not inside the form Quote Link to comment Share on other sites More sharing options...
pctn513 Posted February 15, 2014 Author Share Posted February 15, 2014 Of course! how did I miss such a newbie mistake. Thanks for the advice. 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.