[英]How do I use Ajax to run an SQL query within a php while loop?
I have a table of Data which is pulled via SQL into DataTables. 我有一个Data表,它通过SQL引入DataTables。 I want to use AJAX to run an SQL query which deleted the row based on $id = $row["id"];
我想使用AJAX运行一个SQL查询,该查询根据$id = $row["id"];
删除了行$id = $row["id"];
. 。
Index.php: index.php文件:
$link = mysqli_connect("localhost", "bradlyspicer_root", "", "bradlyspicer_ResellerDB");
$id = $_POST['id'];
$deleterow = "DELETE FROM Offences WHERE id = ?";
if($stmt = mysqli_prepare($link, $deleterow)){ // $link being your connection
mysqli_stmt_bind_param($stmt, "s", $id);
mysqli_stmt_execute($stmt);
echo 'success';
echo $id;
} else {
echo 'fail!';
printf("Error: %s.\n", mysqli_stmt_error($stmt));
}
Functions.php: 的functions.php:
$id = $_POST['id'];
$deleterow = "DELETE FROM Offences WHERE id = ?";
if($stmt = mysqli_prepare($link, $deleterow)){ // $link being your connection
mysqli_stmt_bind_param($stmt, "s", $id);
mysqli_stmt_execute($stmt);
echo 'success';
} else {
echo 'fail!';
printf("Error: %s.\n", mysqli_stmt_error($stmt));
}
Custom.js: Custom.js:
$( ".delbtn" ).click(function(){
var itemID = $(this).attr("itemID");
console.log(itemID)
$.ajax({
url:"functions.php", //the page containing php script
data: { id: itemID}, // itemID passed as id
type: "POST", //request type
success:function(result){
alert(result);
},
error: function() {
alert('Error occured');
}
});
});
I can't find where I pass the $id in the button from Index.php to Functions.php, any explanation would be appreciated. 我无法找到将Index.php中的$ id传递给Functions.php的地方,任何解释都将不胜感激。
Update: Since updating the script and trying to Debug, I'm not getting much of a response from the error which outputs: 更新:自更新脚本并尝试调试以来,我没有从输出的错误得到很多响应:
fail!Error: .
Index.php: index.php文件:
Add a delete button identifier class delbtn
and a data attribute that carries this row's id data-itemID
添加删除按钮标识符类delbtn
和包含此行的id data-itemID
的数据属性
<?php
while($row = mysqli_fetch_array($dataTablesResult)){
$id = $row["id"];
echo '
<tr>
<td>
<button type="button" data-itemID="'.$id.'" class="delbtn btn btn-danger" >Remove</button>
</td>
</tr>
';
}
?>
Functions.php: 的functions.php:
Capture $_POST['id']
sent by ajax 捕获ajax发送的$_POST['id']
$id = $_POST['id'];
$deleterow = "DELETE FROM Offences WHERE id = ?";
if($stmt = mysqli_prepare($link, $deleterow)){ // $link being your connection
mysqli_stmt_bind_param($stmt, "s", $id);
mysqli_stmt_execute($stmt);
}
Custom.js: Custom.js:
Run the jQuery function when a button with .delbtn
class is clicked. 单击带有.delbtn
类的按钮时运行jQuery函数。 Capture and store the row id from data attribute as $(this).data("itemID")
. 从数据属性捕获并存储行id为$(this).data("itemID")
。 Then send the data using data: { id: itemID}
within ajax request 然后使用ajax请求中的data: { id: itemID}
发送数据
$(".delbtn").click(function(){
itemID = $(this).data("itemID");
$.ajax({
url:"functions.php", //the page containing php script
data: { id: itemID}, // itemID passed as id
type: "POST", //request type
success:function(result){
alert(result);
}
});
});
i think if you change this line : 我想如果你改变这一行:
mysqli_stmt_bind_param($stmt, "s", $id);
to this is one: 对此是一个:
mysqli_stmt_bind_param($stmt, "i", $id);
it may works 它可能有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.