Jump to content

Recommended Posts

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");		
	}			
?>
Link to comment
https://forums.phpfreaks.com/topic/286221-help/
Share on other sites

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>
Link to comment
https://forums.phpfreaks.com/topic/286221-help/#findComment-1469077
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.