简体   繁体   中英

Adding delete button into the table

I have to add delete button into each section of the table.

<?php
$link = mysqli_connect("localhost", "root", "", "demo");
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$name = mysqli_real_escape_string($link, $_POST['name']);
$email = mysqli_real_escape_string($link, $_POST['email']);
$contact = mysqli_real_escape_string($link, $_POST['contact']);

$sql = "INSERT INTO persons (name, email, contact) VALUES ('$name', '$email', '$contact')";

if(mysqli_query($link, $sql)){
    echo "<h2>Records added successfully.The new updated records are...</h2><br>";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

$selectData = "select * from persons";
$res = mysqli_query($link, $selectData);
?>

<table border="2">
    <tr>
        <th>id</th>
        <th>Name</th>
        <th>Email</th>
        <th>Contact</th>
    </tr>
</table>

<?php
    if(mysqli_num_rows($res)>0){
        while($row = mysqli_fetch_assoc($res)){
          ?>
            <tr><td><?php echo $row['id'];?></td>
            <td><?php echo $row['name'];?></td> 
             <td><?php echo $row['email'];?></td>
             <td><?php echo $row['contact'];?></td></tr>

         <?php
        }
    }

?>
<tr><th>id</th>
    <th>Name</th>
    <th>Email</th>
    <th>Contact</th>
<th>Delete</th>
</tr>


<?php
    if(mysqli_num_rows($res)>0){
        while($row = mysqli_fetch_assoc($res)){
          ?>
            <tr><td><?php echo $row['id'];?></td>
            <td><?php echo $row['name'];?></td> 
             <td><?php echo $row['email'];?></td>
             <td><?php echo $row['contact'];?></td></tr>
     <td><a onClick="delTopicContentRow('<?php echo $row['id'] ?>')"><img src="../admin_assets/images/delete.png"></a></td>
         <?php
        }
    }

The delete javascript function

function delTopicContentRow(topic_content_id)
{
    var confirm_delete = confirm("Do you want to delete topic content ?");
    if (confirm_delete === true) 
    {
    $(".deleteall"+topic_content_id).addClass("deletealloncomplete");
    $.ajax({
        type:'POST',
        url:'delRow.php',
        data:{topic_content_id:topic_content_id},
        success:function(data)
        {
            if(data == 1)
            {
                $(".deletealloncomplete").fadeOut("slow");
                setTimeout(function(){
                    window.location.reload(1);
                }, 2000);
            }
            else
            {
                alert("Try again.!");
            }
        }
    });
    }
}
     <tr><th>id</th>
<th>Name</th>
<th>Email</th>
 <th>Contact</th>
 <th>Delete</th>
 </tr>

     <?php
       if(mysqli_num_rows($res)>0){
          while($row = mysqli_fetch_assoc($res)){
          ?>
            <tr><td><?php echo $row['id'];?></td>
                 <td><?php echo $row['name'];?></td> 
                 <td><?php echo $row['email'];?></td>
               <td><?php echo $row['contact'];?></td></tr>
               <td><a href="<?php echo 'delete?='.$row['id'] ?>">Delete item</a>
 </td>


           <?php

        }
      }

//This last statement passes the id of the function to delete

function delete(){
   if(isset($_GET['delete'])){
    $id=$_GET['delete'];
  $sql="DELETE  FROM `trial` WHERE `id` ='$id'";
   $stmt=$this->conn->prepare($sql);
  $stmt->execute();

 }

} ?>

你应该从 HTML basic http://www.w3schools.com/html/html_basic.asp开始

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM