0

我有一些来自数据库的数据,我在最后一列中有一个删除按钮。我想删除单击删除按钮的行。但是,每当我单击删除按钮时,即使单击第一行,也只会删除最后一行。

我的删除功能:

if(isset($_POST['formDelete'])){
if(isset($_POST['quoteid']) && !empty($_POST['quoteid'])){
    require_once('config.php');
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE) 
    or die ('Cannot connect to db');
    $quoteid = $_POST['quoteid'];
    echo "DELETE FROM quotes WHERE quoteid =".$quoteid;
    $result = $conn->query("DELETE FROM quotes WHERE quoteid =".$quoteid);
}

表代码:

<form action="<?php $_PHP_SELF ?>" method="post">
<?php
    require_once('config.php');
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE) 
    or die ('Cannot connect to db');
    $result = $conn->query("SELECT quoteid, quote, cat.catid catid, cat.name cat, scat.catid catid, scat.name scat, author FROM quotes q
                           INNER JOIN category cat ON q.catid = cat.catid LEFT OUTER JOIN category scat ON q.subcatid = scat.catid order by quoteid");
    echo "<table border='1px solid black'>";
    echo "<tr><td><b>Category</b></td><td><b>Sub-Category</b></td><td><b>Quote</b></td><td><b>Author</b></td></tr>";
    while ($row = $result->fetch_assoc()) {
        echo "<tr><td>$row[cat]</td><td>$row[scat]</td><td><input type='hidden' id='quoteid' name='quoteid' value=$row[quoteid] />$row[quote]</td><td>$row[author]</td>
        <td><input type='submit' name='formDelete' id='formDelete' value='Delete' /></td></tr>";
    }
    echo "</table>";
?>
</form>

我试图在quoteid的基础上删除。

4

1 回答 1

4

这个想法是为每个删除按钮创建一个单独的表单,以便发送正确的 quoteid 值

<?php
    require_once('config.php');
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE) 
    or die ('Cannot connect to db');
    $result = $conn->query("SELECT quoteid, quote, cat.catid catid, cat.name cat, scat.catid catid, scat.name scat, author FROM quotes q
                           INNER JOIN category cat ON q.catid = cat.catid LEFT OUTER JOIN category scat ON q.subcatid = scat.catid order by quoteid");
?>
    <table border='1px solid black'>
        <tr>
            <td>
                <b>Category</b>
            </td>
            <td>
                <b>Sub-Category</b>
            </td>
            <td>
                <b>Quote</b>
            </td>
            <td>
                <b>Author</b>
            </td>
        </tr>

        <?php
        while ($row = $result->fetch_assoc()) {
            ?>
            <tr>
                <td>
                    <?php echo $row['cat']; ?>
                </td>
                <td>
                    <?php echo $row['scat']; ?>
                </td>
                <td>
                    <?php echo $row['quote']; ?>
                </td>
                <td>
                    <?php echo $row['author']; ?>
                </td>
                <td>
                    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post' style="display: inline;">
                        <input type='hidden' id='quoteid' name='quoteid' value="<?php echo $row['quoteid']; ?>" />
                        <input type='submit' name='formDelete' id='formDelete' value='Delete' />
                    </form>
                </td>
            </tr>
            <?php
        }
        ?>
    </table>
于 2013-05-20T18:07:36.323 回答