0

I'm including a drop down menu and button that deletes the row in a database using php. The value shown in the drop down box (the blog post title) is not the primary key, and the sql delete function only works on the primary key. how can I pass the id to the sql query but still display the title for the blog post?

(yes i do have a connection to the db)

<?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> 
4

1 回答 1

1

You simply put the value in the <option> as follows:

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

This then sends the 1 (or your ID) while still displaying the name.

<option value="<?php echo $blogvar[0]; ?>"><?php echo $blogvar[1]; ?></option>
于 2013-10-14T00:54:37.013 回答