[英]Deleting row from mysql using jquery and php
我想使用功能和jQuery創建刪除功能
我的jquery可以正常工作並顯示消息,但沒有任何反應“ Nothing Deleted”
jQuery代碼
<script type="text/javascript">
$(".remove").click(function(){
var id = $(this).parents("tr").attr("id");
if(confirm('Are you sure to remove this record?'))
{
$.ajax({
url: 'delete.php',
type: 'GET',
data: {id: id},
error: function() {
alert('Something is wrong');
},
success: function(data) {
$("#"+id).remove();
alert("Record removed successfully");
}
});
}
});
PHP功能代碼
function delete($table,$id) {
global $connect;
mysqli_query($connect, "DELETE FROM `$table` WHERE `id` = $id ");
}
Delete.php代碼
include ('function.php');
$id = $_GET['id'];
$table = 'msg';
delete($table,$id);
HTML代碼
<table class="table table-striped" style="background-color: #ffffff;">
<tr>
<th>ID</th>
<th>From</th>
<th>Title</th>
<th>Date</th>
<th>Action</th>
</tr>
<?php
$i = '1';
$username = $user_data['username'];
$query = "SELECT * FROM msg WHERE `go_to` = '$username' Order by id";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $i++; ?></td>
<td><?php echo $row['come_from']; ?></td>
<td>
<a href="read_message/<?php echo $row['id']; ?>"><?php if(count_msg_not_opened($username, $row['id']) > '0')
{
echo $row['title'];
}
else
{
echo '<b>' . $row['title'] . '</b>';
} ?></a></td>
<td><?php echo $row['date']; ?></td>
<td>
<button class="btn btn-danger btn-sm remove">Delete</button>
</td>
</tr>
<?php } ?>
</table>
我還包括“ jquery.min.js”
當我按下“刪除”底部時,此消息出現“您確定要刪除此記錄嗎?”
我按“是”,然后此消息出現“記錄已成功刪除”,但未刪除任何內容。
我不知道問題出在哪里。
您忘記將id
屬性添加到<tr>
<tr id="<?php echo $row['id']; ?>">
您還應該在PHP代碼中添加錯誤檢查和准備好的語句。
您確定已將PHP代碼連接到SQL數據庫嗎?
function delete($table,$id) {
global $connect;
mysqli_query($connect, "DELETE FROM `$table` WHERE `id` = $id ");
}
上面的代碼依賴於您的PHP文件中已經存在的連接。 請參閱此內容以了解如何應用連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.