2013-10-13 43 views
1

數據我是新來的PHP,只是想使一個基本的網頁,我可以看到在數據庫中的所有用戶,並刪除它們。我來了這麼遠,但它一直告訴我,我有一個我有和未定義索引:user_id,雖然它告訴我,它已經刪除了它沒有刪除任何東西的字段。這裏是我的代碼:顯示和刪除數據庫

<?php include_once "includes/scripts.php"; ?> 
<?php include_once "includes/connect.php";?> 
<?php include_once "includes/cms_page_security.php";?> 
<div id="cms_container"><br> 
    <br> 
    <h1>MANAGE USERS<img src="images/three_column_grid_line.png" alt="line"></h1> 
    <p class="logout_btn"><a href="admin_cms.php">Back</a></p> 
<?php 
$tbl="users"; // Table name 
$sql = "SELECT * FROM $tbl"; 
$result = mysql_query($sql, $connect); 
while($rows = mysql_fetch_array($result)){ 
?> 
<?php 
echo $rows['user_id']; 
echo $rows['user_name']; 
echo $rows['user_password']; 
?> 
<a href="delete_user.php?id= <? echo $rows['user_id']; ?>">delete</a> 
<?php 
} 
?> 
<?php 
mysql_close(); 
?> 
</div><!--cms_container--> 
</body> 
</html> 

,它應該鏈接到該頁面刪除查詢:

<?php include_once "includes/connect.php";?> 
<?php 
    $tbl="users"; 
    $user_id= $_GET ['user_id']; 
    $sql="DELETE FROM $tbl WHERE user_id = '$user_id'"; 
    $result = mysql_query($sql, $connect); 
    if($result){ 
     echo "Deleted Successfully"; 
     echo "<BR>"; 
     echo "<a href='delete.php'>Back to main page</a>"; 
    }else { 
     echo "ERROR"; 
    } 
    ?> 
<?php 
mysql_close(); 
?> 
+1

** **危險:您正在使用[**的**過時的數據庫API(http://stackoverflow.com/q/12859942/19068),並應使用[現代更換](HTTP ://php.net/manual/en/mysqlinfo.api.choosing.php)。你也**易受[SQL注入攻擊](http://bobby-tables.com/)**,現代的API會使[防禦]更容易(http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php)自己從。 – Quentin

+0

在你的'delete.php'中'$ connect'在哪裏 –

+0

@Quentin我只是好奇,你有一些*** Danger ***模板嗎? – vikingmaster

回答

1

除了其他答案:

它看起來像這條線可能是一個致命的錯誤,如果PHP短標記未啓用:

<a href="delete_user.php?id= <? echo $rows['user_id']; ?>">delete</a> 

PHP手冊說:

* PHP也允許短標籤<??> (這是氣餒,因爲如果與 short_open_tag的值爲php.ini配置文件中啓用它們只是可用,或者在PHP配置 與 - enable-short-tags選項。* http://php.net/manual/en/language.basic-syntax.phptags.php

0

SQL查詢會成功,即使它改變了零行。您與當生成你的HTML(id= <?)的空間前綴你的用戶ID,這樣你就不會匹配任何行(因爲"1"不會被" 1"匹配)。

0

在其中創建您的「刪除」鏈接

<a href="delete_user.php?id= <? echo $rows['user_id']; ?>">delete</a> 

你正在創建「身份證」的變量,但後來你去找「USER_ID。

更改鏈接

<a href="delete_user.php?user_id=<? echo $rows['user_id']; ?>">delete</a> 
2

你真的應該使用PDO來代替。 問題在於您傳遞的信息。

鏈接:<a href="delete_user.php?id= <? echo $rows['user_id']; ?>">delete</a> 正在尋找一個「身份證」,但你以後尋找「user_ID的」

如果將其更改爲<a href="delete_user.php?user_id=<? echo $rows['user_id']; ?>">delete</a>,它應該工作。

我還是強烈建議你看看PDO來代替,雖然,它更安全,更容易使用。 PDO的

實例刪除

public function deleteUser($username, $user_id){ 

    if($this->isAdmin($username) == true){ 

     $query = $this->db->prepare('DELETE FROM users WHERE user_id = ?'); 
     $query->bindValue(1, $user_id); 

     try{ 
      $query->execute(); 
     }catch(PDOException $e){ 
      die($e->getMessage()); 
     } 
    }else{ 
     return false; 
    } 
} 

我運行一個額外的檢查,以確保誰是請求刪除該人是管理成員,但你應該能夠看到結構

3

在delete_user.php必須因爲在你<a href="delete_user.php?id= <? echo $rows['user_id']; ?>">delete</a>鏈接GET變量得到USER_ID

$user_id= $_GET ['id']; 

是 「ID」,而不是 「user_ID的」