簡體   English   中英

使用PHP變量從MySQL表中刪除條目

[英]Deleting entry from MySQL table using PHP variable

我很確定這個問題已經問過很多遍了,我已經在網上搜索了,但仍然找不到解決該問題的方法。

這是代碼(我知道它不是注入證明):

顯示表中的所有條目

<?php
$query="SELECT * FROM testimony";
$result=mysql_query($query);
while($row=mysql_fetch_array($result))
{
?>
<div>
  <form name="testimonial" action="admintestimony.php" method="post">
  <table border="0">
    <tr>
      <td>Username:</td>
      <td></td>
      <td><input type="text" name="testyname" value='<?php echo $row[0];?>' readonly></td>
    </tr>
    <tr>
      <td>Testimony:</td>
      <td></td>
      <td><textarea name="txttesty" cols="50" rows="10" readonly><?php echo $row[1];?></textarea></td>
    </tr>
    <tr>
      <td><input type="submit" name="approve" value="Approve"></td>
      <td></td>
      <td><input type="submit" name="reject" value="Reject"></td>
    </tr>
  </table>
  </form>
</div>
<?php
}
?>

檢查是否按批准或拒絕

<?php
session_start();
include('connection.php');
$testyname=$_POST['testyname'];
if (isset($_POST['approve']))
{
    header("location:testimonyapproved.php");
}
else if (isset($_POST['reject']))
{
    header("location:reject.php");
}
?>

如果按下拒絕

<?php
session_start();
$testyname=$_POST['testyname'];
include('connection.php');
mysql_query("SELECT * FROM testimony");
mysql_query("DELETE FROM 'transaction' WHERE 'transaction' 'name' = $testyname");
header("location:testimonyrejected.php");
?>

任何幫助,將不勝感激,謝謝。

它是你的SQL

 DELETE FROM `transaction` where `name` = $testyname 

可能會更好地工作

您的SQL格式錯誤(在錯誤的位置引用)。 我在此改進的代碼中添加了一些注釋,請查看。

mysql_query("SELECT * FROM testimony"); //this line is useless and you can delete it
mysql_query("DELETE FROM transaction WHERE name = '".$testyname."'"); //this line contained wrong syntax

請注意,新版本的PHP不推薦使用mysql_函數。 查看此帖子以獲取有關此主題的更多信息。

另外,您可能想防止在$testmyname變量中進行SQL注入。 本指南將為您提供幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM