使用PHP和Javascript删除所选ID的选定行

时间:2017-02-05 23:07:06

标签: javascript php jquery mysql

我试图通过将参数传递到URL来删除一行选定的ID。比方说,我有entryIDs 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);

}

?>

任何建议都将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

你需要为此做最小的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>