2013-06-01 68 views
1

我很確定這個問題已經被問了很多次,我搜索了網頁,仍然找不到解決這個問題的方法。使用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"); 
?> 

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

+0

這很容易注射。對輸入進行清理,並使用mysqli_ *函數而不是mysql_ *棄用的函數。 – wazy

回答

0

,是你的SQL

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

可能會更好地工作

+0

謝謝,但條目仍然存在,它不會刪除。 –

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 

請注意,mysql_函數在新版本的PHP中已被棄用。查詢this post關於此主題的更多信息。

此外,您可能想要防止$testmyname變量中的SQL注入。 This guide將幫助你達到。