【发布时间】:2018-03-09 12:26:32
【问题描述】:
我一直试图弄清楚如何使用复选框和 JavaScript 从 MySQL 中删除多条记录。
此时我有一个工作脚本,它只从我的数据库中删除一条记录(最新 id)。
对于每个product,我都有复选框
<input class="checkboxProduct" type="checkbox" name="deleteProduct" value="<?php echo $product['id'];?>">
我有一个按钮和 JS 表单(工作 - 它获取所有需要的 id,我可以显示它们,但不能删除。
<script>
$(function(){
var e = document.getElementById( "selectAction" );
$("#btn-action").click(function(){
if(e.options[ e.selectedIndex ].value == "delete"){
var checked = $('.checkboxProduct:checked');
var id = checked.map(function() {
return this.value;
}).get().join(",");
if (id) {
checked.closest(".product").remove();
$.ajax({
url: "<?php echo $_SERVER['PHP_SELF']; ?>?deleteProduct=true?action=select&id=" + id,
type: "get",
success: function(result){
alert("You have successfully deleted these products!");
}
});
}
}
});
});
</script>
删除函数:
public function deleteProduct(){
try{
$product_id = $_GET['id'];
$stmt = $this->conn->prepare("DELETE FROM products WHERE id=('$product_id')");
$stmt->execute();
}
catch(PDOException $e){
echo $e->getMessage();
}
}
【问题讨论】:
-
mysite.com/?deleteProduct=true&action=select&id=1%20or%201%3D1%3B%20--%20gonner -
应该是
&action,而不是?action。
标签: javascript php jquery mysql checkbox