使用php删除sql数据库中的行

时间:2013-10-14 00:50:15

标签: php mysql sql

我包含一个下拉菜单和按钮,可以使用php删除数据库中的行。 下拉框(博客文章标题)中显示的值不是主键,sql delete函数仅适用于主键。如何将id传递给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:&nbsp;
                <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> 

1 个答案:

答案 0 :(得分:1)

您只需将值放在<option>中,如下所示:

<option value="1">Volvo</option>

然后在显示名称的同时发送1(或您的ID)。

<option value="<?php echo $blogvar[0]; ?>"><?php echo $blogvar[1]; ?></option>