2011-11-23 114 views
0

編輯

我有一個領域一個MySQL表如下:如何編輯/刪除所存儲的數據從數據庫在PHP

產品 - 系列,名稱,描述,價格,圖片。

的viewproducts.php頁面如下:

<?php 

$result = mysql_query("SELECT * FROM products ") 
or die(mysql_error()); ; 

if (mysql_num_rows($result) == 0) { 
     echo 'There Arent Any Products'; 
    } else { 

echo "<table border='0'><table border='1' width=100%><tr><th>Product Name</th><th>Description</th><th>Price</th><th>Image</th><th>Edit</th><th>Delete</th>"; 
while($info = mysql_fetch_array($result)) 
{ 
     echo "<tr>"; 
     echo "<td>" . $info['name']. "</td>"; 
     echo "<td>" . $info['description']. "</td>"; 
     echo "<td>£" . $info['price']." </td>"; 
     echo "<td>" . "<img src='../getImage.php?id=" . $info['serial'] ."'/></td>"; 

     echo '<td> <a href="edit.php?product_id="' . $info['serial'] . '">Edit</a></td>'; 

} 
    } 
echo "</tr>"; 
echo "</table>"; 
?> 

我edit.php網頁看起來是這樣的:

<?php 

$product_id = $_GET['serial']; 
$result = mysql_query("SELECT * FROM products WHERE serial = '$product_id'") 
or die(mysql_error()); ; 

if (mysql_num_rows($result) == 0) { 
     echo 'There Arent Any Products'; 
    } else { 

echo "<table border='0'><table border='1' width=100%><tr><th>Product Name</th><th>Description</th><th>Price</th><th>Image</th><th>Edit</th><th>Delete</th>"; 
while($info = mysql_fetch_array($result)) 
{ 
     echo "<tr>"; 
     echo "<td>" . $info['name']. "</td>"; 
     echo "<td>" . $info['description']. "</td>"; 
     echo "<td>£" . $info['price']." </td>"; 
     echo "<td>" . "<img src='../getImage.php?id=" . $info['serial'] ."'/></td>"; 

} 
    } 
echo "</tr>"; 
echo "</table>"; 
?> 

當我點擊編輯從THR viewproducts.php頁面,它會到沒有任何內容顯示的edit.php頁面。在地址欄上的序列ID快到了,如下所示:

http://www.********.com/****/admin/edit.php?product_id= 

我希望能夠編輯任何產品從viewproduct.php頁面點擊並轉移到edit.php頁。我不認爲我的edit.php頁面設置正確。

請幫幫忙,

感謝

+0

這意味着你有$ info ['serial']的問題。請做一個var_dump($ info ['serial']);就在編輯線之前,看看你得到了什麼。附:不要忘記清理輸入,否則你會遇到很多sql注入的問題。 –

+0

@AurelioDeRosa對不起,我是新來的PHP,因此失去了你在說什麼。我把var_dump($ info ['serial']);在編輯行之前並在我的服務器上查看它。它提出了字符串(2)「16」字符串(2)「15」字符串(2)「40」之前,我甚至點擊編輯 – Jahed

+0

哦,好吧..我現在做什麼?我無法讓產品出現在edit.php上以允許管理員編輯其內容? – Jahed

回答

0

您可以通過$_GET通過產品的ID,然後,在編輯/刪除頁面,檢索參數。很明顯,您必須在使用之前正確清理輸入。例如,每個產品的鏈接應該是這樣的:

echo '<td><a href="edit.php?id="' . $info['id'] . '">Edit</a></td>'; 

在編輯頁面,你應該有這樣的:

$id = $_GET['id']; 
// For example, if the product id is an integer 
// you can sanitize it doing this 
$id = (int) $id 
+0

我有$ product_id = $ _GET ['serial']; $ result = mysql_query(「SELECT * FROM products WHERE serial ='$ product_id'」)但是我的桌子上沒有任何東西顯示出來當我在網上訪問這個時,序列號爲空 – Jahed

+0

@清除更新您的問題,向我們展示如何更改代碼。 –

+0

對不起,我現在編輯了我的主要帖子。我希望現在它更有意義 – Jahed

0

你可以把它作爲在一個參數傳遞給你的PHP文件至極要編輯/刪除產品:

<a href="edit.php?product_id=<?php echo $row['product_id']; ?>">Edit Product</a> 

然後你edit.php你會拿起產品的ID,並加載它的數據庫中的數據。

[edit.php] 
$product_id = isset($_GET['product_id']) ? intval($_GET['product_id']) : null; 
if(!$product_id) { 
    exit(); 
} 
// query your database for the product 
$row = mysqli_query("SELECT * FROM <table> WHERE product_id = $product_id"); 
// then you output your html with fields populated from the result from the database