5

Delete the selected row from the database

 2 years ago
source link: https://www.codesd.com/item/delete-the-selected-row-from-the-database.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Delete the selected row from the database

advertisements

I would like to delete a row from mysql database. The row I would like to delete is the 'selected item' in the combobox.

My code so far:

    $sql = "SELECT data_id FROM projects";
    $result = mysql_query($sql);

    echo "<select name='data_id '>";
    echo "<option selected>Please select...</option>";
    while ($row = mysql_fetch_array($result))
    {
        echo "<option value='" . $row['data_id '] . "'>" . $row['data_id'] . "</option>";
    }
    echo "</select>";

    ?>
    <tr><td><input type="submit" name="delete" value="Delete" id="delete"></td></tr></tr></table>
    <?php
     //delete selected item from projects table...
    ?>

The combobox is showing all the items from my projects table. When I select an item, then click on 'delete' I would like to delete that item from the table.

Any help will be appreciated.

zpNnx.png

I did not get why do they suggest you to use ajax, while you can make simple form

<form method="post" action="">
<tr><td>
//SOME PHP where you obtain from the database the data_id (this you have done already)
    <option value='" . $row['data_id '] . "'>" . $row['data_id'] . "</option>
//
<input type="submit" name="delete" value="Delete" id="delete">
</td></tr>
</form>

and on same page at the top before HTML

<?php
if(isset($_POST['delete']) {
//and now mysql DELETE FROM .... WHERE id = $_POST['data_id']
//dont forget to escape and use mysli instead of mysql
}
?>

Btw. dont use mysql_* its deprecated, + escape everything in mysql + dont use fetch_array when you dont need an array, fetch_assoc is fine ;) Hope that helps.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK