Delete data

  1. delete_form.php

<!DOCTYPE html>
<html>
<head>
	<title>Delete Record</title>
</head>
<body>
	<h2>Delete Record</h2>

	<?php
	// Include the database connection file
	include_once 'dbconfig.php';

	// Check if the id parameter is set in the URL
	if(isset($_GET['id'])){
		$id = $_GET['id'];

		// Select the record from the database
		$sql = "SELECT * FROM users WHERE id = $id";
		// $result = mysqli_query($conn, $sql);
		$result = mysqli_query($mysqli, $sql);

		if(mysqli_num_rows($result) == 1){
			$row = mysqli_fetch_assoc($result);
			?>

			<form method="post" action="delete.php">
				<p>Are you sure you want to delete this record?</p>
				<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
				<p><strong>Name: </strong><?php echo $row['name']; ?></p>
				<p><strong>Email: </strong><?php echo $row['email']; ?></p>
				<input type="submit" name="submit" value="Delete">
			</form>

			<?php
		} else {
			echo "Record not found.";
		}
	} else {
		echo "Invalid request.";
	}
	?>
</body>
</html>
  1. delete.php

<?php
// Include the database connection file
include_once 'dbconfig.php';

// Check if the id parameter is set in the URL
if(isset($_POST['id'])){
	$id = $_POST['id'];

	// Delete the record from the database
	$sql = "DELETE FROM users WHERE id = $id";
	if(mysqli_query($mysqli, $sql)){
		echo "Record deleted successfully.";
	} else {
		echo "Error deleting record: " . mysqli_error($mysqli);
	}
} else {
	echo "Invalid request.";
}

// Close the database connection
mysqli_close($mysqli);
?>

Last updated