1

我编写了删除 MySQL 表行的代码。但是当我点击删除图标时,什么也没有发生。有人可以告诉我我的代码中还剩下什么吗?

<?php
include_once 'include/DatabaseConnector.php';
$query1="SELECT * FROM MyTable;";
$result1=DatabaseConnector::ExecuteQueryArray($query1);
?>

<script type="text/javascript">
function deleteRow(tableName,colName,id){
    $.ajax({
           type: "POST",
           url: "delete.php",
           data: "tableName=tableName&colName=colName&id=id",
           success: function(msg){
             alert( "Row has been updated: " + msg );
           }
    });
}
</script>

<table id="newspaper-b" border="0" cellspacing="2" cellpadding="2" width = "100%">
<thead>
<tr>
    <th scope="col">Opr</th>
    <th scope="col">Flt Num</th>
    <th scope="col">From</th>
    <th scope="col"></th>
</tr>
</thead>
<tbody>
<?php foreach ($result1 as $row):?>
<tr>
<td><?php echo $row['airlineName'];?></td>
<td><?php echo $row['flightNum'];?></td>                        <td><?php echo $row['from'];?></td>
<td>
  <div title='Delete' onclick='deleteRow(<?php echo 'flightschedule','flightNum',$row['flightNum']; ?>)'>
<img src='images/delete.png' alt='Delete' />
</div>              
</td>
</tr>
<?php endforeach;?>
</tbody>

删除.php

<?php
    /* Database connection */
    include_once 'include/DatabaseConnector.php';
    if(isset($_POST['tableName']) && isset($_POST['colName']) && isset($_POST['id'])){
        $tableName = $_POST['tableName'];
        $colName = $_POST['colName'];
        $id = $_POST['id'];
        $sql = 'DELETE FROM '.$tableName.' WHERE '.$colName.' ="'.$id.'"';
        mysql_query($sql);
    } else { 
        echo '0'; 
    }
?>
4

3 回答 3

1
  • 你检查你的PHP日志看是否有错误?
  • 什么是Ajax.Request?如果您使用的是原型库,那么它包含在您的 HTML 代码中的什么位置?
  • 最后,你确定你的 PHP 代码被调用了吗?(检查使用例如 Chrome 浏览器中的 Web 开发工具,“请求”选项卡)
于 2012-05-24T12:13:51.033 回答
0

此行有错误

 <div title='Delete' onclick='deleteRow(<?php echo 'flightschedule','flightNum',$row['flightNum']; ?>)'>

这将打印

 <div title='Delete' onclick='deleteRow(flightscheduleflightNumid)'>

您必须将其更改为

  <div title='Delete' onclick='deleteRow("flightschedule","flightNum",<?php $row['flightNum']; ?>)'>

去工作。

最好的祝愿

更新:要使上述代码正常工作,还请添加

<script src="js/jquery.js" type="text/javascript" ></script>

或从互联网上加载

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js"></script>  

在 html 的标题中包含 Jquery。我已经测试了它现在可以了。

于 2012-05-24T13:11:47.873 回答
0

您不会将您的数据发送到 delete.php 文件,因为在日期属性中您严重抓住了它们。这是我刚刚测试过他(它)的这段代码,这似乎有效。

数据:"tableName="+tableName+"&colName="+colName+"&id="+id+"",

function deleteRow(tableName,colName,id){
    $.ajax({
           type: "POST",
           url: "delete.php",
           data: "tableName="+tableName+"&colName="+colName+"&id="+id+"",
           success: function(msg){
             alert( "Row has been updated: " + msg );
           }
    });
}

于 2018-04-01T14:17:42.503 回答