使用复选框从数据库表中删除行 [英] Deleting a row from a database table using checkbox
问题描述
我想在表格中显示来自数据库的记录,每行中都有复选框。该复选框将确定用户是否要删除该特定行(如果将被选中)。我能够显示从数据库的数据,但当我按删除按钮没有发生。我不知道,但我认为错误是在我删除的代码部分,但我可能是错误的。我不确定。无论如何,这里是代码,我只是删除一些行
<?php
$ link = mysql_connect(localhost,root,123);
if(!$ link){
die('无法连接到服务器:'。mysql_error());
}
$ db = mysql_select_db(abc);
if(!$ db){
die(无法选择数据库);
}
$ 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('该条目不存在'); 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>学生记录< / caption>
< th colspan ='1'>学生号码< / th>
< th colspan ='8'> $ searchbox< / th>
< / tr>
< tr>
< th>删除< / th>
&th;学生号< / th>
< th>学院< / th>
< th>课程< / th>
< th>状态< / th>
< th>姓氏< / th>
" th>名字< / th>
< th>中间名< / th>
< th>地址< / th>
< th>性别< / th ;
< th>公开状态< / th>
< th>宗教< / th>
< th>电子邮件地址< / th>
&第< / th>
< th>天< / th>
< th>年< / th>
< th父亲姓名< / th>
< th>父亲的职业< / th>
< th>母亲姓名< / th>
< th>母亲姓名< / 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 =checkboxid =checkbox [< ;? echo $ rows ['id']; ?>]value =< ;? echo $ rows ['id']; ?>"< / td>
<?php
echo< td>。$ row ['studentnumber']。< / td>;
echo< td>。$ row ['college']。< / td>;
echo< td& ;;
echo< td>。$ row ['status']。< / td> 。< / td>;
echo< td>。$ row ['fname']。 row ['mname']。< / td>;
echo< td>。$ row ['address']。< / td>;
echo< ; td>。$ row ['gender']。< / td>;
echo< td>。$ row ['civilstatus']。 b $ b echo< td>。$ row ['religion']。< / td>;
echo< td>。$ row ['emailaddress']。 / td>;
echo< td>。$ row ['month']< / td& ']。< / 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;
默认值:
echo< script type ='text / javascript'> alert('An error occurred'); location.href ='admin_home.php';< script>;
}
?>
< input name =deletetype =submitid =deletevalue =Delete>
<?php
//检查删除按钮是否激活,启动此
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 Delee'); location.href ='admin_home.php';< / script&
exit();
}
mysql_close();
?>非常感谢! 解决方案当一个特定复选框被选中或取消选中时写一个JavaScript函数,并将逗号分隔列表中的id存储为一个隐藏变量,然后在页面被发布之后u可以获取您想要的字段的id使用Explode删除并拆分变量以删除逗号。
I want to display a record from a database in a table with checkboxes in every row. That checkboxes will determine if the user wants to delete that specific row if it will be checked. I was able to display the data from the DB but when i press the delete button nothing happen. Im not sure but i think the error is in my deleting part of the code but i could be wrong. Im not sure. Anyway, here is the code, i just remove some lines
<?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();
?>
Thanks in advance !!
解决方案 Write a JavaScript function when a particular check box is checked or unchecked and store the ids in a comma separated list as a hidden variable then after the page is posted u can get id's of fields you want to delete and split the variable using "Explode" to remove comma.
这篇关于使用复选框从数据库表中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!