[英]Can't delete a row from a Database using a img PHP
我正在為我的網站做后台,並顯示數據,我從數據庫中帶了一張表來顯示用戶請求的所有升級,並且我正在考慮放置2張圖像,一個用於更新,另一個用於刪除同一行。
# Table from Database #
?>
<div id="DBForm">
<p id="title" align="center"> Upgrades</p>
<br/>
<?php
$resultado=mysql_query("SELECT * FROM upgrades");
if (mysql_affected_rows()>=1)
{
echo "<table class='DBTable'>";
echo "<tr>
<th>Nome</th>
<th>Upgrade</th>
<th>Morada</th>
<th>Contacto</th>
<th>NIF</th>
<th>Factura</th>
<th>Data</th>
<th>N Serie</th>
<th>Cupao</th>
<th>Alterar</th>
<th>Eliminar</th>
</tr>";
while ($linha=mysql_fetch_array($resultado))
{
echo "<tr align='center'>
<td>".$linha["nome"]."</td>
<td>".$linha["upgrade"]."</td>
<td>".$linha["morada"]."</td>
<td>".$linha["contacto"]."</td>
<td>".$linha["nif"]."</td>
<td>".$linha["factura"]."</td>
<td>".$linha["data_factura"]."</td>
<td>".$linha["n_serie"]."</td>
<td>".$linha["cupao"]."</td>
<td><a href=\"alterar.php\"><img src=images/alterar.png></img></a> </td>
<td><a href=\"eliminar.php\"><img src=images/eliminar.png></img></a> </td>
</tr>";
}
echo "</table>";
}
?>
</div>
沒關系,但是當我按img刪除時,瀏覽器說我成功刪除了該行,但是當我返回頁面時該行仍然存在,那可能是什么?
#Code from img to delete the row#
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "loja";
$tbl_name = "upgrades";
// Create connection
mysql_connect($servername, $username, $password)or die ("Não houve conexão");
mysql_select_db("$dbname");
$id=$_REQUEST['id'];
$sql="DELETE FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
if ($result)
{
echo "Registo apagado com sucesso!";
echo "<br>";
echo "<a href='upgrades.php'> Voltar a upgrades </a>";
}
else
{
echo "ERRO!";
mysql_close();
}
?>
沒有提供此值:
$id=$_REQUEST['id'];
該頁面只需要一個鏈接即可:
"<a href=\"eliminar.php\"><img src=images/eliminar.png></a>"
為了提供標識符,您需要將其添加到鏈接中。 也許是這樣的:
"<a href=\"eliminar.php?id=".$linha["id"]."\"><img src=\"images/eliminar.png\"></a>"
(或$linha
上的任何標識符)
此外,請注意,您的代碼對SQL注入攻擊開放。 您將要研究使用准備好的語句,而不是將用戶輸入直接放入SQL代碼中。
旁注:您可以安全地刪除</img>
標簽; 這不是有效的標簽。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.