php, mysql - delete item from dropdown and database

I have a dropdown menu. I want to delete a selected item from the dropdown and the database.

For clarity, I outsourced the database connection instructions and call them in the necessary places with - require ("db.inc.php");.

I have written a class and method to delete the selected article (a variable). I call the method in the file - testdelete.php. And via a button in the index file.

There must, however, be an error in the transfer of the variables. I think the passage with the form is grotty. - I can use the script to delete an article, but not the one currently selected.

Does anyone have an idea or solution? That would be great

The Index-File:

<!-- class, function -->
<?php
class artikel {
    private $table = "artikel";

    public function delete_a($id) {
   // connection to the db
require("db.inc.php");
//
 $sql = "DELETE FROM " .$this->table ." WHERE anr = ?";
 if ($stmt = $mysqli -> prepare($sql)) {
    $stmt->bind_param('i', $id);
    $stmt -> execute();
  }
  $stmt->close();
  $mysqli->close(); 
}
}
?>
<!-- -->
<?php 
// connection to the database
require("db.inc.php");
// SQL command
$sql = "SELECT anr, name FROM artikel";
// prepared Statements
    if ($stmt = $mysqli -> prepare($sql)) 
        { 
            $stmt -> execute(); 
            $stmt -> bind_result($anr, $name); 
?>
<!--form, select and option -->
        <form action="" method="POST">
            <label for="artikel">Artikel</label>
            <select id="artikel" name="artikel">

<?php
            while ($stmt -> fetch()) 
            {
                echo "<option value=\"\">"
                    . $anr 
                    . "|" 
                    . $name
                    . "</option>\n\t";
            }
?>
            </select>
            <p>
                  <a href="testdelete.php?anr=<?php echo $anr; ?>">
                  <input type="button" value="Delete article"></a>
                  </p>
       </form>
<?php                  
            $stmt->close(); 
        } 
        $mysqli->close();   
?>

Connection to the db (db.inc.php)

<?php
$mysqli = new mysqli("localhost", "root", "xxx", "myDB");
if ($mysqli->connect_error) {
  echo "You see an error: " . $mysqli->connect_error;
  exit();
}
if (!$mysqli->set_charset("utf8")) {
  echo "UTF8 don't work: ". $mysqli->error;
}
?>

And the function call delete in a external file named testdelete.php:

<?php 
// function call
    if(isset($_GET["anr"])) {
    $artikel = new artikel();
    $artikel -> delete_a($_GET["anr"]);
    echo "<h2>Article deleted</h2>";
    }
    header("refresh:3; url=index.php");
?>

1 answer

  • answered 2017-11-12 20:53 YouneL

    Try To use post request instead, and you have to pass an action to your form, try this code:

    The Index-File:

    <?php 
    // connection to the database
    require("db.inc.php");
    // SQL command
    $sql = "SELECT anr, name FROM artikel";
    // prepared Statements
        if ($stmt = $mysqli -> prepare($sql)) 
            { 
                $stmt -> execute(); 
                $stmt -> bind_result($anr, $name); 
    ?>
    <!--form, select and option -->
            <form action="testdelete.php" method="POST"> 
                <label for="artikel">Artikel</label>
                <select id="artikel" name="artikel">
    
    <?php
                while ($stmt -> fetch()) 
                {
                    // here you have to pass a value to your select
                    echo "<option value=\"$anr\">"
                        . $anr 
                        . "|" 
                        . $name
                        . "</option>\n\t";
                }
    ?>
                </select>
                <p>
                    <!-- here you have to use submit button in order to post your selected value -->
                    <input type="submit" value="Delete article">
                </p>
        </form>
    <?php                  
                $stmt->close(); 
            } 
            $mysqli->close();   
    ?>
    

    testdelete.php

    <?php 
    // function call
        if(isset($_post["artikel"])) { // that's because your select name is "arikel"
        $artikel = new artikel();
        $artikel -> delete_a($_post["artikel"]);
        echo "<h2>Article deleted</h2>";
        }
        header("refresh:3; url=index.php");
    ?>