2013-06-01 42 views
-1

我有從數據庫中刪除數據的問題,我做了一個表,它將顯示註冊用戶的名稱和密碼。並在他們旁邊有一個刪除鏈接。 當我按下刪除鏈接,它給了我一個錯誤。從數據庫中刪除數據在php中

這裏是viewTheUsers.php頁

<html> 
<head> 
<title>the users</title> 
</head> 
<body> 
<table width ="650" align ="center" border ="2" >  
<tr> 
<th width="60">User No </th>  
<th>User Name </th> 
<th>User Password </th> 
<th>Delete User </th> 
</tr>  
<?php 
mysql_connect("hostName","userName","password"); 
mysql_select_db("db_name"); 
$Thequery = "SELECT * FROM table_name"; 
$run = mysql_query($Thequery); 
while($row = mysql_fetch_array($run)){ 
$The_id = $row[0];  
$The_name = $row[1]; 
$The_pass = $row[2]; 
?> 
<tr> 
<td><?php echo $The_id; ?></td>  
<td><?php echo $The_name; ?></td> 
<td><?php echo $The_pass; ?></td> 
<td><a href="delete.php?deleting=<?php echo $The_id; ?>">Delete</a></td> 
</tr> 
<?php }?> 
</table> 
</body> 
</html> 

這裏是delete.php頁:

<?php 
mysql_connect("hostName","userName","password"); 
mysql_select_db("db_name"); 
$deleting_id = $_GET['deleting']; 
$Thequery = "delete from users where id = '$deleting_id' "; 
if (mysql_query($Thequery)){ 
echo "<script>window.open('viewTheUsers.php?deleted = user has been deleted!!!','_self')</script>";  
} 
?> 

的massege錯誤說:

公告:未定義索引:在C:\ wamp \ www \ delete中刪除.php on line 6

+0

你知道mysql。*函數被折舊嗎? – imulsion

+1

delete.php的第6行是什麼? – Barmar

+3

這個錯誤表明你有'$ _GET ['del']'在你的腳本代替'$ _GET ['刪除']'檢查輸入錯誤 – Barmar

回答

1

請注意,此代碼存在許多問題。其中之一是注入問題,你只需將get變量中的所有內容直接放入數據庫查詢中即可。此外,該window.open調用打開一個無效的網址(我想它可能會工作,但你應該做的空間等網址編碼)。要挑剔,你的變量命名實際上是非標準的(第一個字母大部分是小寫,爲什麼要在每個地方加上'''',但這不是真正的biggy)。

最後,檢查第6行。錯誤的意思是「你正試圖訪問一個數組中的值,但你正在使用的索引('del')根本不存在。 $_GET['del']並且它沒有設置

+0

我很抱歉,我得到的消息錯誤是:注意:未定義的索引:在第6行刪除C:\ wamp \ www \ delet.php所以它刪除而不是刪除 – user2442835

+2

請考慮剪切/粘貼,這個通知也有一個錯字。雖然這一點是相同的:'$ _GET ['deleted'];'不存在。 'var_dump'你的數組,檢查你的url,並且修正你的delete ID是空的事實。可能是某處的錯字。現在開始調試,您知道通知的原因。 – Nanne