2013-10-06 34 views
0

我想刪除已添加的數據庫中的一行,但是當我嘗試單擊我的index.php中的刪除按鈕時,它說刪除了記錄,但是當我單擊確定時,未刪除的記錄,這裏是我的代碼:使用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

您是否收到任何錯誤消息? – TAAPSogeking

+0

嘗試更改此行「to 。 onclick部分可能會與帖子混淆 – TAAPSogeking

回答

0

修改下面一行

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

由於

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

您的ID未在delete.php文件中獲取所以只要刪除您的刪除按鈕,並用此代碼替換:

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

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

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