将删除按钮添加到表中

时间:2017-01-17 10:17:15

标签: php html mysql

我必须在表格的每个部分添加删除按钮。

<?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
        }
    }

?>

3 个答案:

答案 0 :(得分:0)

<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
        }
    }

删除javascript功能

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.!");
            }
        }
    });
    }
}

答案 1 :(得分:0)

     <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

        }
      }

//最后一条语句传递要删除的函数的id

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

 }

}        ?&GT;

答案 2 :(得分:0)

您应该从HTML basic http://www.w3schools.com/html/html_basic.asp

开始