我对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();
我想要做的是,在表的右侧或左侧有一列复选框,底部有一个按钮,所以我可以选中一个框,然后从数据库中删除数据。
如何为在while循环中找到的每个条目添加一个复选框(这将是一个新的<td>
条目)?
对此有任何帮助非常感谢!
谢谢大家!
答案 0 :(得分:0)
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>";
这样做,它会在表格右侧添加一个复选框,每个复选框都有不同的名称(我假设你的表的'name'列具有唯一值)。
Q2:选择并删除这些条目 答:为此您可以添加此脚本
<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代替。我已经使用过它们,这样你就可以理解解决这个问题的过程了。
答案 1 :(得分:0)
没有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的值,以便更好地理解。