2012-02-23 53 views
1

我們無法獲取刪除「提交」按鈕進行設置,否則會起作用,我們無法理解哪部分代碼是錯誤的。提交按鈕不會設置

想象有幾列,其中從數據庫中包含數據的TD的

// While it goes through each row (which is selected from another SQL query above, but I won't bother pasting that) 
while ($row = mysql_fetch_array($result)) { 
// Variable is equal to the Request ID (e.g. 10) 
$numb = $row['ReqID']; 

// Echo's the table td rows with each column name in the database 
echo "<tr>"; 
echo "<td class='req' align='center'>".$row['ReqID']."</td>"; 
echo "<td class='req' align='center'>".$row['ModTitle']."</td>"; 
echo "<td class='req' align='center'>".$row['BuildingID']."</td>"; 
echo "<td class='req' align='center'>".$row['RoomID']."</td>"; 
echo "<td class='req' align='center'>".$row['Priority']."</td>"; 
echo "<td class='req' align='center'>".$row['W1']."</td>"; 
echo "<td class='req' align='center'>".$row['P1']."</td>"; 
// Delete button also in a td row, uses the variable "$numb" to set the button name 
echo "<td class='req' align='center'><input type='submit' name='{$numb}' value='Delete'></td>"; 
echo "</tr>"; 

// If the delete button is pressed, delete the row (this query works, we tested it, but the button just doesn't set so it doesn't activate the SQL command) 
if (isset($_POST[$numb])) { 
$result2 = mysql_query("DELETE FROM Request WHERE ReqID = {$numb} LIMIT 1", $connection); 
} 
} 
+1

你爲什麼用{}包裝$麻煩? – 2012-02-23 17:01:35

+2

我假設整個事物都圍繞着'

'標籤,對吧? – papaiatis 2012-02-23 17:02:35

+0

與您的問題無關,但該代碼在刪除後會顯示已刪除的行......是否爲預期行爲? – bfavaretto 2012-02-23 17:05:40

回答

1

使用hidden輸入存儲值錶行:

以上地方......

<form method="post" action="delete.php"> 

稍微低於...

echo "<td class='req' align='center'><input type='submit' name='submit' value='Delete'>"; 
echo '<input name="hello" type="hidden" value="$numb" /></td>'; 
... 
$number = mysql_real_escape_string($_POST["hello"]); 
$result2 = mysql_query("DELETE FROM Request WHERE ReqID = ".$number." LIMIT 1", $connection); 

底部:

</form> 

注:

你的做法是不夠安全的。我可以輕鬆地編輯DOM併爲隱藏字段(或按鈕的另一個名稱)提供另一個值,從而刪除數據庫中的其他元素。請記住這一點。

+0

你打算'echo' ...''? – 2012-02-23 17:11:23

+0

是的,剛剛更新了我的代碼,但它也不是更好。如果它只是一個ID,我仍然可以從數據庫中刪除其他東西...我注意到他的方法本身很糟糕。 – papaiatis 2012-02-23 17:15:33

+0

好點。 +1。 – 2012-02-23 17:39:01