我想在表格中的数据库中显示记录,每行都有复选框。该复选框将确定用户是否要删除该特定行(如果要检查)。我能够显示数据库中的数据但是当我按下删除按钮时没有任何反应。我不确定,但我认为错误是我删除部分代码,但我可能是错的。我不确定。无论如何,这是代码,我只是删除一些行
<?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();
?>
提前致谢!!
答案 0 :(得分:1)
您可以在复选框上使用值,并将它们用作数组,这样您就可以知道哪一个已经过检查。
<input type="checkbox" name="need_delete[$id]">
这样,您的$_POST['need_delete']
将收到一个以您的ID为索引的数组,因此您将能够测试并删除这些寄存器。
答案 1 :(得分:1)
看起来您正在尝试访问$_POST
数组中的错误索引。请参阅以下内容,该内容取自PHP代码的末尾。
if ( ! empty($_POST['delete'])) {
应该是
if ( ! empty($_POST['need_delete'])) {
答案 2 :(得分:1)
我做了类似的事情来显示以及追加/删除记录。我使用的方法是遍历PHP脚本中的所有post vars,并根据键的第一部分搜索所有信息(如果它匹配某些内容,请使用之后的数字作为对象id)。然后遍历生成的数据数组,这些数据将被追加,删除的内容,编辑的内容以及被忽略的内容。
如果对象已经存在并且未设置复选框,请忽略 如果对象已存在且数据不同且未设置复选框,则更新 如果对象已存在且已设置复选框,请删除 如果对象不存在且未设置复选框,请创建新行 如果对象不存在且设置了复选框,则忽略
希望有所帮助
有一些代码
PHP Block:
$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" width="900" 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" width="16" height="16" border="0" alt="Pick a date"></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" width="16" height="16" border="0" alt="Pick a date"></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" alt="Cancel" 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 :(得分:1)
在选中或取消选中特定复选框时编写JavaScript函数,并将id作为隐藏变量存储在逗号分隔列表中,然后在页面发布后,您可以获取要删除的字段的id并使用以下内容拆分变量“爆炸”以删除逗号。