gpt4 book ai didi

php - 使用复选框从数据库表中删除一行

转载 作者:可可西里 更新时间:2023-11-01 08:07:25 26 4
gpt4 key购买 nike

我想在表格中显示数据库中的一条记录,每行都有复选框。该复选框将确定用户是否要删除该特定行(如果选中)。我能够显示来自数据库的数据,但是当我按下删除按钮时,什么也没有发生。我不确定,但我认为错误出在我删除部分代码中,但我可能是错的。我不确定。无论如何,这是代码,我只是删除了一些行

 <?php

$link = mysql_connect("localhost","root", "123");
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}

$db= mysql_select_db("abc");
if(!$db) {
die("Unable to select database");
}


$search = $_POST['search'];
$searchbox = $_POST['searchbox'];


$query = ("SELECT $search FROM table where $search = '$searchbox'");
$result=mysql_query($query);

if($result)
{
if(mysql_num_rows($result)<=0)
{
echo "<script type='text/javascript'>alert('The entry does not exist'); location.href = 'admin_home.php';</script>";
}
}



switch ($search)
{
case 'studentnumber':
$result = mysql_query("SELECT * FROM table WHERE $search = '$searchbox'");
if($result){

echo "<table class='hovertable'>
<tr>
<caption>Student Records</caption>

<th colspan='1'> Student Number</th>
<th colspan='8'> $searchbox</th>
</tr>

<tr>
<th>Delete</th>
<th>Student Number</th>
<th>College</th>
<th>Course</th>
<th>Status</th>
<th>Last Name</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Address</th>
<th>Gender</th>
<th>Civil Status</th>
<th>Religion</th>
<th>Email Address</th>
<th>Month</th>
<th>Day</th>
<th>Year</th>
<th>Father's Name</th>
<th>Father's Occupation</th>
<th>Mother's Name</th>
<th>Mother's Name</th>
</tr>";


while($row = mysql_fetch_array($result))
{
echo "<tr onmouseover=\"this.style.backgroundColor='#ffff66';\" onmouseout=\"this.style.backgroundColor='#d4e3e5';\">";

?>
<td><input name="need_delete[<? echo $rows['id']; ?>]" type="checkbox" id="checkbox[<? echo $rows['id']; ?>]" value="<? echo $rows['id']; ?>"></td>
<?php
echo "<td>" . $row['studentnumber'] . "</td>";
echo "<td>" . $row['college'] . "</td>";
echo "<td>" . $row['course'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['mname'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['civilstatus'] . "</td>";
echo "<td>" . $row['religion'] . "</td>";
echo "<td>" . $row['emailaddress'] . "</td>";
echo "<td>" . $row['month'] . "</td>";
echo "<td>" . $row['day'] . "</td>";
echo "<td>" . $row['year'] . "</td>";
echo "<td>" . $row['father'] . "</td>";
echo "<td>" . $row['fatheroccupation'] . "</td>";
echo "<td>" . $row['mother'] . "</td>";
echo "<td>" . $row['motheroccupation'] . "</td>";
}

echo "</table>";
echo "</div>";
}
break;

default:
echo "<script type='text/javascript'>alert('An error occur'); location.href = 'admin_home.php';</script>";
}

?>
<input name="delete" type="submit" id="delete" value="Delete">
<?php
// Check if delete button active, start this
if ( ! empty($_POST['delete'])) {
foreach ($_POST['need_delete'] as $id => $value) {
$sql = 'DELETE FROM students` WHERE id='.(int)$id;
mysql_query($sql);
}
echo "<script type='text/javascript'>alert('Record Successfully Deleted');location.href = 'admin_home.php';</script>";
exit();
}
mysql_close();
?>

提前致谢!!

最佳答案

您可以在复选框上使用值,并将它们用作数组,这样您就可以知道哪个已被选中。

<input type="checkbox" name="need_delete[$id]">

这样做,您的 $_POST['need_delete'] 将收到一个以您的 ID 作为索引的数组,因此您将能够测试和删除这些寄存器。

关于php - 使用复选框从数据库表中删除一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8870741/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com