如何使用ajax调用从数据库中删除表行

时间:2017-12-02 10:38:32

标签: javascript php jquery mysql ajax

我有一个带有php + mysql的表,当我点击删除按钮时,它被删除但是在重新刷新页面后,同样的信息被加载到表中,

所以删除特定行后,如何永久删除数据库中正确的ID自动更改。我哪里弄错了?

提前致谢。

<script type="text/javascript">

$(document).ready(function()
{
    $('.delete').click(function()
    {
        if (confirm("Are you sure you want to delete this row?"))
        {
            var id = $(this).parent().parent().attr('id');
            var data = 'id=' + id ;
            var parent = $(this).parent().parent();
 
            $.ajax(
            {
                   type: "POST",
                   url: "add_edit1.php",
                   data: data,
                   cache: false,
 
                   success: function()
                   {
                    parent.fadeOut('slow', function() {$(this).remove();});
                   }
             });
        }
    });
 
    // style the table with alternate colors
    // sets specified color for every odd row
    $('table#delTable tr:odd').css('background',' #FFFFFF');
});





</script>	
<div class="table-responsive">
                    <?php
                    // Include config file
                    require_once 'config.php';
                    
                    // Attempt select query execution
                    $sql = "SELECT * FROM illt";
                    if($result = mysqli_query($link, $sql)){
                        if(mysqli_num_rows($result) > 0){
							
                            echo "<table class='table table-bordered table-striped table-condensed'>";
                                echo "<thead>";
                                    echo "<tr>";
                                        echo "<th>#</th>";
                                        echo "<th>name</th>";
                                        echo "<th>bandwidth</th>";
                                        echo "<th>connectivity</th>";
                                        echo "<th>popname</th>";
										echo "<th>popip</th>";
										echo "<th>port</th>";
										echo "<th>vlan</th>";
										echo "<th>nms</th>";
                                    echo "</tr>";
                                echo "</thead>";
                                echo "<tbody>";
								$count = 1;
                                while($row = mysqli_fetch_array($result)){
                                    echo "<tr>";
                                        echo "<td>" . $row['id'] . "</td>";
                                        echo "<td>" . $row['name'] . "</td>";
                                        echo "<td>" . $row['bandwidth'] . "</td>";
                                        echo "<td>" . $row['connectivity'] . "</td>";
										echo "<td>" . $row['popname'] . "</td>";
										echo "<td>" . $row['popip'] . "</td>";
										echo "<td>" . $row['port'] . "</td>";
										
										echo "<td>" . $row['nms'] . "</td>";
                                        echo "<td>";
										
										    echo "<a target = '_blank' href='http://maps.google.com/?q=". $row['latitude'].','. $row['longitude']. "' title='Mapping' data-toggle='tooltip'><i class='material-icons'>&#xE0C8;</i></a>";
                                            echo "</td>";
											
											echo "<td>";
											echo "<a href='readi.php?id=". $row['id'] ."'  title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
                                            echo "<a href='updatei.php?id=". $row['id'] ."'  title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
                                            echo "<a href='deletei.php?id=". $row['id'] ."' data-toggle='modal' data-target='#exampleModalLong'  title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
                                            echo "<span class='delete' id='del_<?php echo ". $row['id'] ."; ?>'>Delete</span></a>";
   
										echo "</td>";
                                    echo "</tr>";
									
									
									$count++;
									}
									
									
									
                                echo "</tbody>";                            
                            echo "</table>";
                            // Free result set
                            mysqli_free_result($result);
                        } else{
                            echo "<p class='lead'><em>No records were found.</em></p>";
                        }
                    } else{
                        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
                    }
 

                    // Close connection
                    mysqli_close($link);
                    ?>
					
                </div>

*附加edit1.php

<?php 
include "config.php";

if($_POST['id'])
{
$id=mysqli_real_escape_string($_POST['id']);
$delete = "DELETE FROM `illt` WHERE id='$id'";
mysqli_query( $delete);
}

?>
*

1 个答案:

答案 0 :(得分:0)

  1. 如果您使用的是mysqli,mysqli_query()中的第一个参数必须为$conn
  2. 尝试将if($_POST['id'])更改为if(isset($_POST['id']))
  3. 在WHERE子句中 id 应为`id`
  4. 尝试将mysqli_query()的结果设置为变量(例如$result)。接下来,检查$result等于false,如果是,则按函数mysqli_error($result)
  5. 获取最后一个mysqli错误

    最后,您的代码应为

    <?php
    include "config.php";
    if(isset($_POST['id'])){
        $id=mysqli_real_escape_string($_POST['id']);
        $delete = "DELETE FROM `illt` WHERE `id`='$id'";
        $result = mysqli_query($connection, $delete);
        if(!$result){
            echo "MySQLi error: ". mysqli_error($result);
        }
    }
    ?>
    

    *如果这对您有用,请为其评分

相关问题