我包含一個下拉菜單和按鈕,用於刪除數據庫中使用php的行。 下拉框(博客文章標題)中顯示的值不是主鍵,sql刪除功能僅適用於主鍵。我怎麼能傳遞ID到SQL查詢,但仍顯示博客文章的標題?使用php刪除sql數據庫中的行
(是的,我確實有到數據庫的連接)
<?php
$delete = $_POST['delete_submit'];
mysql_query("DELETE FROM blogpost WHERE BlogPostID=$delete");
?>
<section style="font-size: 22px; text-align: center;">
<form action='admin.php' method='post' onsubmit="return confirm('Do you really want to delete this post?');">
<p>
Delete Post:
<select name="delete">
<option>Please select a post</option>
<?php
$result = mysql_query("SELECT BlogPostID, BlogPostTitle FROM blogpost ORDER BY BlogPostID DESC");
while ($row = mysql_fetch_array($result)){
$blogvar = array($row ['BlogPostID'], $row ['BlogPostTitle']);
?>
<option><?php echo $blogvar[1]; ?></option>
<?php
};
?>
</select>
<p></p>
<input type='submit' class='button' name='delete_submit' value='Delete'/>
</p>
</form>
</section>
Fluffeh提供了這種機制,但在發送給db之前,您仍然應該清理收到的值。 – Tarnation