[英]Delete Selected row of selected ID using PHP and Javascript
我正在嘗試通過將參數傳遞到URL來刪除所選ID的行。 假設我有entryID 1和2,每當我嘗試選擇和刪除條目1的內容時,它都會成功刪除entryID 1的內容,但是問題是當我選擇刪除entryID 2時,它仍會刪除entryID 1而不是2我在想變量var row = '".$rows['Blog_ID']."';
不會改變,即使我選擇其他方式,它也只會保留entryID 1的值。
這是到目前為止我嘗試過的
<?php
include("../Connection.php");
$post_query="Select * from indexview order by Blog_ID Desc";
$postsql=mysqli_query($connect_db,$post_query) or die('Connection unsuccessful');
while($rows=mysqli_fetch_array($postsql,MYSQL_ASSOC)){
echo "<div id='posts'>";
echo" <select onchange = 'down(this.value)' id='downpng' name='downpng'>
<option value='void'></option>
<option value = 'edit'>Edit Blog</option>
<option value ='delete'>Delete</option>
</select>";
echo
"<script>
function down(temp) {
var row = ".$rows['Blog_ID'].";
var id = '".$_GET['id']."';
if(temp=='delete'){
var con = confirm('Are you sure?');
if(con){
window.location = 'google.php?entryID=' + row + '&id=' + id;
}else{
window.location = '../Blog/Blog.php?id=".$_GET['id']."';
}
}else{
window.location = '../Blog/edit.php';
}
}
</script>";
當我選擇<option value ='delete'>Delete</option>
,應該將我重定向到deleteBlog.php頁面並刪除所選entryID的內容。
deleteBlog.php代碼:
<?php
include("../Connection.php");
if(isset($_GET['entryID'])){
$user = $_GET['id'];
$entry = $_GET['entryID'];
mysqli_query($connect_db, "Delete from blog_tbl where Blog_ID=" .$entry);
header('Location: ../Blog/Blog.php?id='.$user);
}
?>
任何建議將不勝感激。 謝謝!
您需要為此做最少的php,尤其是在javascript部分。 只需存儲博客ID(我將其存儲在select屬性的名稱中)並通過javascript提取即可。 我將使用jQuery做JS東西。
<?php
# Include database
include("../Connection.php");
# Create a simple function that does not use id="downpng" (id values are
# supposed to be unique
function getOrderDropDown($con)
{
$query = "Select * from indexview order by Blog_ID Desc";
$postsql = mysqli_query($con,$query) or die('Connection unsuccessful');
$str = '';
while($rows=mysqli_fetch_array($postsql,MYSQL_ASSOC)){
$str .= "
<select name='downpng[".$rows['Blog_ID']."]' class='blog_select'>
<option value='void'></option>
<option value = 'edit'>Edit Blog</option>
<option value ='delete'>Delete</option>
</select>";
}
return $str;
}
# Write the selects to page
echo getOrderDropDown($connect_db);
?>
提取選擇的Javascript:
<script>
// I would only do php here, use a special chars, otherwise you will be easily hacked by user input
var id = <?php echo (!empty($_GET['id']))? '"'.htmlspecialchars($_GET['id'],ENT_QUOTES).'"' : 'false' ?>;
// On change of this class type
$('.blog_select').on('change',function(e) {
// Get the name (which contains the id)
var row = $(this).attr('name').replace(/[^0-9]/gi,'');
// This will be the action (delete, edit)
var action = $(this).val();
// On delete, assign the actions and send values
if(action == 'delete'){
var redirect;
var con = confirm('Are you sure?');
if(con){
redirect = 'google.php?entryID='+row+'&id='+id;
}else{
redirect = '../Blog/Blog.php?id='+id;
}
}else{
redirect = '../Blog/edit.php';
}
// Just do one redirect
window.location = redirect;
});
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.