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

标签 php mysql

我有一个下拉菜单。我想从下拉列表中删除选定的项目,并数据库

为了清楚起见,我外包了数据库连接指令,并在必要的地方用 -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 的外部文件中的删除:

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

最佳答案

尝试使用发布请求,并且您必须将操作传递到表单,请尝试以下代码:

索引文件:

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

关于php,mysql - 从下拉列表和数据库中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47253585/

相关文章:

php - 搜索并替换数千行 php

javascript - 将 Web 服务响应转换为 PDF 文件 - PHP、Javascript

mysql - chef_gem 的依赖项

PHP PDO类编程:Fatal error: Call to a member function fetchAll() on boolean

php - Web 应用程序 - 生物识别系统连接

php - 将最后一行连接到另一行

php - 如何根据条件制作出现管道

java - 无法连接到远程MySQL服务器

php - 如何使用 PHP 将 MySQL 数据行放在适当的标题下?

mysql - 如何使用mysql中存储的任意数据特征训练DNN分类模型?