使用复选框从数据库表中删除一行
Posted
技术标签:
【中文标题】使用复选框从数据库表中删除一行【英文标题】:Deleting a row from a database table using checkbox 【发布时间】:2012-02-10 20:19:47 【问题描述】:我想在表格中显示数据库中的一条记录,每行都有复选框。该复选框将确定用户是否要删除该特定行(如果将被选中)。我能够显示数据库中的数据,但是当我按下删除按钮时没有任何反应。我不确定,但我认为错误在于我删除了部分代码,但我可能是错的。我不知道。无论如何,这是代码,我只是删除了一些行
<?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();
?>
提前致谢!!
【问题讨论】:
【参考方案1】:在选中或取消选中特定复选框时编写一个 JavaScript 函数,并将 id 作为隐藏变量存储在逗号分隔列表中,然后在页面发布后,您可以获取要删除的字段的 id 并使用拆分变量“Explode”删除逗号。
【讨论】:
【参考方案2】:我做了类似的事情来显示和附加/删除记录。我使用的方法是遍历 PHP 脚本中的所有 post var,并根据键的第一部分搜索所有信息(如果匹配,则使用后面的数字作为对象 id)。然后循环遍历生成的数据数组,这些数据将被追加,删除什么,编辑什么,忽略什么。
如果对象已按原样存在且未设置复选框,则忽略 如果对象已存在且数据不同且未设置复选框,则更新 如果对象已存在且已设置复选框,则删除 如果对象不存在且未设置复选框,则创建新行 如果对象不存在且已设置复选框,则忽略
希望有帮助
有一些代码
PHP 块:
$mysqlData = array(); // Items to Update
$mysqlNewData = array(); // Items to Create
$Data = $_POST;
unset($Data['stuff']);
foreach($Data as $k => $v)
$type = substr($k, 0, 2);
if ($type == "nw")
$type = substr($k, 2, 2);
$ID = substr($k, 4);
$mysqlSubData = $mysqlNewData[$ID];
if (gettype($mysqlSubData) != "array")
$mysqlSubData = array();
if ($type == "sd")
$mysqlSubData['startdate'] = $v;
if ($type == "ed")
$mysqlSubData['enddate'] = $v;
if ($type == "dl")
if ($v == "on")
$mysqlSubData['delete'] = true;
$mysqlNewData[$ID] = $mysqlSubData;
else
$ID = substr($k, 2);
$mysqlSubData = $mysqlData[$ID];
if (gettype($mysqlSubData) != "array")
$mysqlSubData = array();
if ($type == "sd")
$mysqlSubData['startdate'] = $v;
if ($type == "ed")
$mysqlSubData['enddate'] = $v;
if ($type == "dl")
if ($v == "on")
$mysqlSubData['delete'] = true;
$mysqlData[$ID] = $mysqlSubData;
表格块:
<? $occurences = $mysql->getEntries("occurences", "`TargetID`='$Target['ID']'"); ?>
<table cellpadding="0" cellspacing="5" border="0" id="timetable">
<tr>
<td>Delete</td>
<td>Start Date</td>
<td>End Date</td>
</tr>
<? foreach($occurences as $occurence) ?>
<? $ID = $occurence['ID']; ?>
<tr>
<td>
<input type="checkbox" name="dl<?=$ID?>" value="on" />
</td>
<td>
<input type="text" name="sd<?=$ID?>" id="sd<?=$ID?>" value="<?=$occurence['startdate']?>" style="width: 100px" />
<a href="javascript:;" onclick="javascript:NewCssCal('sd<?=$ID?>','yyyyMMdd','arrow')"><img src="/scripts/cal.gif" border="0" ></a>
</td>
<td>
<input type="text" name="ed<?=$ID?>" id="ed<?=$ID?>" value="<?=$occurence['enddate']?>" style="width: 100px" />
<a href="javascript:;" onclick="javascript:NewCssCal('ed<?=$ID?>','yyyyMMdd','arrow')"><img src="/scripts/cal.gif" border="0" ></a>
</td>
<td>
</td>
</tr>
<? ?>
</table>
<a href="javascript:;" onclick="AddOccurence();">Add Another</a>
<div style="text-align: center">
<input type="image" src="/images/button_save.png" value="Save" border="0" />
<a href="/"><img src="/images/button_cancel.png" border="0" /></a>
</div>
</form>
<script type="text/javascript">
var TableRowExtras = 0;
function AddOccurence()
TableRowExtras++;
var da = new Date();
var t = document.getElementById('timetable');
var row = t.insertRow(t.rows.length);
var cell = row.insertCell(0);
var element = document.createElement('input');
element.name = 'nwdl' + TableRowExtras;
element.type = 'checkbox';
element.value = 'on';
cell.appendChild(element);
cell = row.insertCell(1);
element = document.createElement('input');
element.type = 'text';
element.name = 'nwsd' + TableRowExtras;
element.id = 'nwsd' + TableRowExtras;
element.value = da.getFullYear() + '-' + (da.getMonth() + 1) + '-' + da.getDate();
element.style.width = '100px';
cell.appendChild(element);
cell.innerhtml = cell.innerHTML + " ";
element = document.createElement('a');
element.href = "javascript:NewCssCal('nwsd" + TableRowExtras + "','yyyyMMdd','arrow')";
var element2 = document.createElement('img');
element2.src = '/scripts/cal.gif';
element2.width = '16';
element2.height = '16';
element2.border = '0';
element2.alt = 'Pick a date';
element.appendChild(element2);
cell.appendChild(element);
cell = row.insertCell(2);
element = document.createElement('input');
element.type = 'text';
element.name = 'nwed' + TableRowExtras;
element.id = 'nwed' + TableRowExtras;
element.style.width = '100px';
cell.appendChild(element);
cell.innerHTML = cell.innerHTML + " ";
element = document.createElement('a');
element.href = "javascript:NewCssCal('nwed" + TableRowExtras + "','yyyyMMdd','arrow')";
element2 = document.createElement('img');
element2.src = '/scripts/cal.gif';
element2.width = '16';
element2.height = '16';
element2.border = '0';
element2.alt = 'Pick a date';
element.appendChild(element2);
cell.appendChild(element);
</script>
【讨论】:
【参考方案3】:您似乎正在尝试访问 $_POST
数组中的错误索引。请参阅以下内容,该内容取自您的 PHP 代码末尾附近。
if ( ! empty($_POST['delete']))
应该是
if ( ! empty($_POST['need_delete']))
【讨论】:
【参考方案4】:您可以在您的复选框上使用值,并将它们用作一个数组,这样您就会知道哪个被选中了。
<input type="checkbox" name="need_delete[$id]">
这样做,您的$_POST['need_delete']
将收到一个以您的 id 作为索引的数组,因此您将能够测试和删除这些寄存器。
【讨论】:
以上是关于使用复选框从数据库表中删除一行的主要内容,如果未能解决你的问题,请参考以下文章