簡體   English   中英

使用php刪除數據庫中的一行

[英]Deleting a row in a database using php

我想刪除數據庫中已添加的行,但是當我嘗試單擊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>

我缺少什么,應該刪除和添加什么?

您的ID不在delete.php文件中,因此只需刪除您的刪除按鈕,並使用以下代碼替換即可:

<td><a href="delete.php? id=<?php echo $row['id'];?>">Delete</td>

修改以下行

mysql_query("DELETE FROM schedule WHERE deltime='$deltime'");

mysql_query("DELETE FROM schedule WHERE deltime='$deltime'", $con);

嘗試通過一個鏈接將日期發送到delete.php

<td><a href = 'delete.php'?date=<?php echo $row['time'];?>>DELETE</a></td>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM