2009-10-14 68 views
1

試圖在這裏執行一個非常簡單的任務。

我有一個<ol>包含4行數據在一些方便<li> s。我想添加一個刪除按鈕來從表格中刪除該行。 delete.php中的腳本似乎已經完成,但是當我返回並檢查dashboard.php和PHPMyAdmin以獲取列表時,該行從未被刪除。

下面是刪除按鈕的代碼(PHP內):

Print "<form action=delete.php method=POST><input name=".$info['ID']." type=hidden><input type=submit name=submit value=Remove></form>"; 

移動到delete.php:

<? 
//initilize PHP 

if($_POST['submit']) //If submit is hit 
{ 
    //then connect as user 
    //change user and password to your mySQL name and password 
    mysql_connect("mysql.***.com","***","***") or die(mysql_error()); 

    //select which database you want to edit 
    mysql_select_db("shpdb") or die(mysql_error()); 

    //convert all the posts to variables: 
    $id = $_POST['ID']; 


    $result=mysql_query("DELETE FROM savannah WHERE ID='$id'") or die(mysql_error()); 

    //confirm 
    echo "Patient removed. <a href=dashboard.php>Return to Dashboard</a>"; 
} 
?> 

數據庫是:shpdb 表是:大草原

想法?

+0

你能打印即回聲 「DELETE FROM WHERE大草原ID = '$ ID'」;看看$ id是否真的被替換的值。 – jatanp 2009-10-14 23:55:26

+6

此代碼易受SQL注入攻擊。 http://php.net/manual/en/security.database.sql-injection.php – 2009-10-14 23:58:24

+0

此外,你必須確保人們不能通過猜測id's來刪除任意的東西。如果這是用戶相關的數據,什麼阻止我猜測ID和刪除其他人的信息。 – BeWarned 2009-10-15 00:47:38

回答

7

它拒絕堅持,因爲你把它稱爲一件事,並與另一件事。變化:

"<input name=".$info['ID']." type=hidden>" 

"<input name=ID value=".$info['ID']." type=hidden>" 

因爲delete.php你想與訪問它:

$id = $_POST['ID']; 

你真的應該引用屬性值以及即:

print <<<END 
form action="delete.php" method="post"> 
<input type="hidden" name="ID" value="$info[ID]"> 
<input type="submit" name="submit" value="Remove"> 
</form> 
END; 

或甚至:

?> 
form action="delete.php" method="post"> 
<input type="hidden" name="ID" value="<?php echo $info['ID'] ?>"> 
<input type="submit" name="submit" value="Remove"> 
</form> 
<? 
+0

謝謝cletus。我知道這很簡單。 – 2009-10-14 23:59:17

3

對於網絡的愛,請不要自己構建SQL查詢。使用PDO

+0

感謝您的支持。 – 2009-10-15 00:28:03

+0

鼓勵我做一些研究......謝謝! – 2009-10-15 00:37:51

1

我想說的另一點。我95%確定你不能給一個輸入一個數字名稱/ id屬性。它必須像「id_1」而不是「1」。你也可以用數組來做數組。

所以,你可以做到這一點

<input name="delete[2]"> 

然後在你的PHP

if(isset($_POST['delete'])) 
    foreach($_POST['delete'] as $key=>$val) 
    if($_POST['delete'][$key]) delete from table where id = $val 
相關問題