php,mysql - 从下拉列表和数据库中删除项目

时间:2017-11-12 20:27:31

标签: php mysql

我有一个 下拉菜单 。我想从 下拉菜单中 删除 所选的 项目 < / strong> 数据库

为了清楚起见,我将数据库连接指令外包,并在 - require(“db.inc.php”);。

的必要位置调用它们。

我编写了一个类和方法来删除所选文章(变量)。我在文件中调用该方法 - testdelete.php。并通过索引文件中的按钮。

但是,变量传输必定存在错误。我觉得这个形式的段落很糟糕。 - 我可以使用脚本删除文章,但不能删除当前选中的文章。

有没有人有想法或解决方案? 那会很棒

索引文件:

<!-- 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();   
?>

与数据库的连接(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;
}
?>

该函数在名为testdelete.php的外部文件中调用delete:

<?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");
?>

2 个答案:

答案 0 :(得分:0)

尝试使用post请求,并且必须将操作传递给表单,请尝试以下代码:

索引文件:

<?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");
?>

答案 1 :(得分:0)

好的。这个小脚本不需要定义类。这适用(适用于数据库信息和php文件的名称)。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<?php
// function for dropdown, select
function dataSelect($db) {
    $dropdown = "<select name=\"anr\" id=\"anr\">\n"; 
    $sql = "SELECT * FROM article"; 
    if ($stmt = $db -> prepare($sql)) { 
        $stmt -> execute();
        if ($result = $stmt->get_result()) {    
            while ($row = $result->fetch_object()) {
                $dropdown .= "<option value=\"".$row->anr."\"\n"; 
                $dropdown .= ">" .$row->anr." | " .$row->name." </option>"; 
            }
        }
    }       
    else    {
        return false;
    }
    $dropdown .= "</select>\n";
    return $dropdown; 
}
// \ 

// try DB
$db = new mysqli("localhost", "root", "", "examplDB");
if ($db->connect_error) {
    echo "Error: ".$mysqli->connect_error;
    exit();
}
if (!$db->set_charset("utf8")) { 
echo "Error -> UTF8 ". $mysqli->error;
}
// \ 

// form
echo "<form action=\"\" method=\"POST\">
      <label for=\"anr\">Article: </label>\n";
echo dataSelect($db); // select
echo "<input type=\"submit\" name=\"Delete\" value=\"Data delete\"     />\n"; 
echo "</form>";
// \ 

// delete Data
if (isset($_POST['Delete']))        {
    $sql = "DELETE FROM article WHERE anr = ?"; 
    if ($stmt = $db -> prepare($sql)) { 
        $stmt->bind_param('i', $_POST['anr']);
        $stmt -> execute();
    }
    echo "<h1>Data deleted</h1>";
    header("refresh:3;url=_self_");
}
$stmt->close();
$mysqli -> close();
?>
</body>
</html>