使用PHP变量从MySQL表中删除条目

时间:2013-06-01 00:38:44

标签: php mysql

我很确定这个问题已被多次询问,我在网上搜索过,但仍无法找到解决此问题的方法。

这是代码(我知道它不是注射证明):

显示表格中的所有条目

<?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");
?>

任何帮助都将不胜感激,谢谢。

2 个答案:

答案 0 :(得分:0)

它是你的SQL

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

可能会更好用

答案 1 :(得分:0)

您的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_函数。有关此主题的更多信息,请查看this post

另外,您可能希望阻止$testmyname变量中的SQL注入。 This guide会帮助你。