我想創建一個使用功能和jQuery刪除功能刪除行從MySQL使用jQuery和PHP
我的jQuery的作品和表演的消息,但沒有發生「沒有刪除」
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」
當我按下 「刪除」 底部會出現這條消息:「你一定要刪除這個記錄?「
我按下「是」,出現此消息「記錄已成功刪除」,但沒有任何內容被刪除。
我不知道問題出在哪裏。
PHP代碼中沒有任何內容檢查「DELETE」是否成功或刪除了任何東西。 – Barmar
你可以做得更好。如果你通過'0; drop table msg;'作爲id,你所有的行將被立即刪除!不要使用mysqli。永遠。改用PDO,並閱讀SQL注入。 http://php.net/manual/en/book.pdo.php – Peter
@Peter不,它不會。你將不得不調用mysqli_multi_query()來工作。 – Barmar