我想删除我添加的数据库中的一行,但是当我尝试单击index.php中的删除按钮时,它表示已删除记录但是当我单击确定时,它不会删除记录,这里是我的代码:
index.php
:
$con = mysql_connect("localhost","pma");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("class_schedule", $con);
$result = mysql_query("SELECT * FROM schedule");
?>
<center>
<table border="1" width="800">
<tr>
<td colspan='8'><input type="text" width='300'/>
<input type="button" value="Search" onclick="location='search.php'" />
<input type="button" value="View All" onclick="window.location.href=''"/>
<input type="button" value="Add" onclick="location='add.php'"/></td>
</tr></table>
<?php
echo "<center><table border='1' width='800'>
<tr>
<td><center>Time</td>
<td><center>Subject</td>
<td><center>Course</td>
<td><center>Section</td>
<td><center>Day</td>
<td><center>Room</td>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['time'] . "</td>";
echo "<td>" . $row['subject'] . "</td>";
echo "<td>" . $row['course'] . "</td>";
echo "<td>" . $row['section'] . "</td>";
echo "<td>" . $row['day'] . "</td>";
echo "<td>" . $row['room'] . "</td>";
?>
<td><input type="submit" value="Edit" name="edit" />
<form action="delete.php" method="post">
<input type="submit" value="Delete" name="delete" onclick="location='delete.php'" />
</td>
</form>
<?php
echo "</tr>";
}
echo "</table></center>";
mysql_close($con);
?>
这是我的delete.php
:
<?php
require "connect.php";
$deltime=$_POST["deltime"];
mysql_query("DELETE FROM schedule WHERE deltime='$deltime'");
mysql_close($con);
?>
<div>
<p align="center"><b>Record Deleted</b><br/>
<form method="post">
<center><input type="submit" name="submit" value="Ok" formaction="index.php" /></center>
</form>
</p></div></form>
我缺少什么,应该删除和添加什么?
答案 0 :(得分:1)
您的ID未进入delete.php
文件,因此请删除删除按钮并替换为此代码:
<td><a href="delete.php? id=<?php echo $row['id'];?>">Delete</td>
答案 1 :(得分:0)
修改以下行
mysql_query("DELETE FROM schedule WHERE deltime='$deltime'");
作为
mysql_query("DELETE FROM schedule WHERE deltime='$deltime'", $con);
答案 2 :(得分:0)
尝试通过一个链接将日期发送到delete.php
<td><a href = 'delete.php'?date=<?php echo $row['time'];?>>DELETE</a></td>