-4

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.

  • 6
    You can't use JS (client) variable in PHP (server) this way... Study AJAX little bit more. – pavel Jan 02 '21 at 18:13
  • You don't need jQuery for such a simple task - use vanilla JS: https://blog.garstasio.com/you-dont-need-jquery/ajax/ – AbsoluteBeginner Jan 02 '21 at 19:38
  • 1
    It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Jan 03 '21 at 13:08
  • 1
    Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Dharman Jan 03 '21 at 13:09

1 Answers1

-1

In $.ajax - dont send student_name In php - $text - what is it, where it came from ?

Try

$.ajax({
        type: "POST",
        url : "delete.php",
        data: {'studen_name':$row} ,
        success: function(data){
                      alert('Send request for delete');
                  }
        
    }); 

In delete.php

<?php

include 'adm_complaints.php';
$host = "localhost";
$port = 3306;
$socket = "MySQL";
$user = "root";
$password = "";
$dbname = "hostel_management";

if (isset($_POST['studen_name']) && $_POST['studen_name'] != '') {
    $mysqli = new mysqli($host, $user, $password, $dbname, $port, $socket);
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    if ($stmt = $mysqli->prepare("delete from complaints where student_name=?")) {
        $stmt->bind_param("s", $_POST['studen_name']);
        $stmt->execute();
        printf("%d Row deleted.\n", $stmt->affected_rows);
        $stmt->close();
    }
    $mysqli->close();
} else {
    echo 'No parameter';
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
WiatroBosy
  • 1,076
  • 1
  • 6
  • 17
  • Code corrected - free of sql injection and die () – WiatroBosy Jan 03 '21 at 14:25
  • Good job. I formatted the code to look nicer. One more thing you should be aware of is that we try to teach all users to enable mysqli error reporting. In case you don't know how to do that, you can check here [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) This would make your answer even better. – Dharman Jan 03 '21 at 14:35
  • It should also write class and not write procedural and use the MVC framework and the user should not be "root" and the js script should be in a separate file and should use htmlspecialchars in drawing the table and should use include_onec instead of include. (> ‿◠) ✌ – WiatroBosy Jan 03 '21 at 14:56
  • Yes, as you can see answering such low-quality questions is generally not a good idea. They are rarely useful to us. It's better to spend our efforts on better questions; it's more rewarding. – Dharman Jan 03 '21 at 15:22
  • I fixed the code - shouldn't reputation come back? – WiatroBosy Jan 03 '21 at 16:25
  • I don't know who downvoted your post. They might not come back to see you have edited it. – Dharman Jan 03 '21 at 16:56