我想创建一个使用功能和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