将复选框添加到 html table 以便能够从 sql 数据库中删除数据

Adding checkboxes to an html table to then be able to remove data from sql database

我对 php/sql 很陌生..享受我到目前为止所学的知识,我现在对此感到困惑..

这是我当前的代码:

$query = "SELECT * FROM beings"; 
$result = mysql_query($query);
echo "<table><tr><th>Name</th><th>Location</th><th>Question</th><th>Time Submitted</th></tr>";
while ($row = mysql_fetch_array($result)) {   
    echo "<tr><td>" . $row['name'] . "</td><td>" . $row['location'] . "</td><td>" . $row["question"] . "</td><td>" . $row["submitted"] ."</td></tr>";
}
echo "</table>";
mysql_close();

我想做的是在 table 的右侧或左侧有一列复选框,底部有一个按钮,这样我就可以选中一个框,然后将其删除来自数据库的数据。

如何让它为它在 while 循环中找到的每个条目添加一个复选框(这将是一个新的 <td> 条目)? 非常感谢对此的任何帮助! 谢谢大家!

Q1:如何添加复选框? 答:echo "<tr><td>" . $row['name'] . "</td><td>" . $row['location'] . "</td><td>" . $row["question"] . "</td><td>" . $row["submitted"] ."</td><td><input type='checkbox' value='".$row['name']."' name='".$row['name']."'> </td></tr>";

它的作用是,它会在 table 的右侧添加一个复选框,并且每个复选框都有不同的名称(我假设 table 的 'name' 列具有唯一值)。

Q2:Select 并删除那些条目 答:为此你可以添加这个脚本

<script> 

$("#delete_form").on('submit',function(e){
    e.preventDefault();
    $.ajax({
        url:"delete_names.php",
        type:"POST",
        data:$("#delete_form").serialize(),
        success: function(response){
            $('#response').html(response);
        }
    });
});

HTML 页面将像

    <form method="POST" id="delete_form">
    <?php
    $query = "SELECT * FROM beings"; 
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)){   
        echo "<tr><td>" . $row['name'] . "</td><td>" . $row['location'] . "</td><td>" . $row["question"] . "</td><td>" . $row["submitted"] ."</td><td><input type='checkbox' value='".$row['name']."'  name='".$row['name']."'> </td></tr>";
    }
    echo "</table>";
    mysql_close();

    ?>
    <input type="submit" id="#btn" value="Sumbit" />
    </form>
    <div id="response" > </div>

而 delete_name.php 会像

<?php 
    //make connection to the database
    $con=mysqli_connect("localhost","my_user","my_password","my_db");
    foreach($_POST as $key=>$value){
        $query= "DELETE from beings WHERE name = ".mysqli_real_escape_string($con,$key);
        $result = mysql_query($query);
    }
?>

PS:您需要停止使用 mysql_ 函数,因为它们已被弃用。使用 mysqli_/PDO instead.I 已经使用过,所以你可以理解解决这个问题的过程。

没有jQuery

的解决方案

如果您不想使用 jQuery,则将 "action" 添加到表单中,作为具有表单和复选框的相同 php 页面。

<form method="POST"  action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<?php
$query = "SELECT * FROM beings"; 
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){   
    echo "<tr><td>" . $row['name'] . "</td><td>" . $row['location'] . "</td><td>" . $row["question"] . "</td><td>" . $row["submitted"] ."</td><td><input type='checkbox' value='".$row['name']."'  name='".$row['name']."'> </td></tr>";
}
echo "</table>";
mysql_close();

?>
<input type="submit" id="#btn" value="Sumbit" />
</form>

并且提交此表单时,表单数据将发布到此 php 文件本身,因此您需要在此页面顶部添加表单处理和行删除代码(在 html 表格).

<?php 
//make connection to the database
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if(isset($_POST)){
    //if say there were 2 checkboxes checked with entries as 'a'and 'b'before submiting the form then $_POST['a']='a' and $_POST['b']='b'
    foreach($_POST as $key=>$value){
    //inside this loop we will make a query which will delete the name one by one
    //in the first iteration the $query will become
    //   DELETE from beings WHERE name = 'a'
        $query= "DELETE from beings WHERE name = ".mysqli_real_escape_string($con,$key);
        $result = mysql_query($query);
        // 'mysqli_real_escape_string' function is used to prevent SQL injection.That is a separate and very important topic.This function just say sanitize the data coming from user(which may or may not be tampered)
    }
}
?>

尝试在 for 循环中打印 $key $value 和 $query 的值以便更好地理解。