簡體   English   中英

PHP Ajax MySQL表行刪除

[英]PHP Ajax MySQL Table Row Delete

我編寫了刪除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>

delete.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'; 
    }
?>
  • 您是否檢查過PHP日志以查看是否存在錯誤?
  • 什么是Ajax.Request 如果您使用的是原型庫,那么它在HTML代碼中包含在哪里?
  • 最后,您確定您的PHP代碼被調用了嗎? (例如,使用Chrome瀏覽器中的“ Web開發人員工具”的“請求”標簽進行檢查)

您在此行有一個錯誤

 <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。 我已經測試過了。

您不會將數據發送到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 ); } }); } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM