I want to delete rows from the database using jQuery. I have retrieved the database table using PHP and want to delete the row from the database when I click the delete button.
Here is my code. adm_complaints.php
<?php include('register.php') ?>
<!DOCTYPE html>
<html>
<head><title>AMC Hostel</title>
</head>
<body>
<table id="t02">
<tr class="header">
<th>Student name</th>
<th>Complaint</th>
<th>Date of complaint</th>
<th></th>
</tr>
<tbody>
<?php
$sql="select student_name, complaint, date_of_complaint from complaints";
$result = $conn->query($sql);
?>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<tr role="row" class="complaints_row">
<td class="name"><?php echo $row['student_name']; ?></td>
<td><?php echo $row['complaint']; ?></td>
<td><?php echo $row['date_of_complaint']; ?></td>
<td><button class="show-name">Delete</button></td>
</tr>
<?php } ?>
</table>
<script>
$(".show-name").click(function(){
var $row = $(this).closest("tr");
var $text = $row.find('.name').text();
$.ajax({
url : "delete.php",
success: function(data){
alert('Directory created');
}
});
});
</script>
Here, I want the row to be deleted from the MySQL database when I click the delete button. Here is my delete.php
<?php
include 'adm_complaints.php';
$host="localhost";
$port=3306;
$socket="MySQL";
$user="root";
$password="";
$dbname="hostel_management";
$conn = new mysqli($host, $user, $password, $dbname, $port, $socket)
or die ('Could not connect to the database server' . mysqli_connect_error());
$resolve="delete from complaints where student_name='$text'";
$resolved=mysqli_query($conn, $resolve) or die(mysqli_error($conn));;
?>
When I click the delete button alert box is showing 'directory created' but the row is not deleting in the database. I'm new to JQuery, so please tell me even if I made any silly mistakes. Thanks in advance.