php - 如何在 PHP 和 MySQL 上使用下拉菜单同时删除和插入

标签 php mysql drop-down-menu

我在尝试同时使用下拉菜单插入和删除时遇到问题

<div id="blabla">
    <div style="position: relative;">
        <p style="position: absolute; top: 185px; left: 58px;">  
            <select name="lista">
                <option selected="selected"></option>
                <optgroup label="Selecciona">
                    <option> Option 1</option>
                    <option> Option 2</option>
                    <option> Option 3</option>
                    <option> Option 4</option>
                    <option> Option 5</option>
                </optgroup>
            </select>

这是 ADD.php 代码

   <html>
<head>
</head>
<body>

 <?php
if(isset($_POST['justifica']) && !empty($_POST['justifica']) &&
isset($_POST['si']) && !empty($_POST['si']) &&
isset($_POST['lista']) && !empty($_POST['lista']))
{
mysql_connect("123", "123", "p123") or die(mysql_error()) ; 
mysql_select_db("123") or die(mysql_error()) ; 

mysql_query("INSERT INTO dos (lista,justifica,si) VALUES ('$_POST[lista]','$_POST[justifica]','$_POST[si]')");
mysql_query("DELETE FROM dos2 WHERE lista5='$_POST[lista]')");
if ($_POST['value'] === '') {
    $_POST['value'] = null; // null en mayuscula si es SQL
}
echo "<img src=imagenes/Satisf.jpg>";
}else{
echo "<img src=imagenes/Error.jpg>";
}
?>

</body>
</html>

我想INSERT到表名“dos”中,同时在按下“提交”按钮时从表名“dos2”中DELETE

在表单“Option1”上,将在表“dos”上插入“Option1”,同时从表“dos2”中删除“Option1”。

最佳答案

我会使用 MySQLi 来完成此操作,并且我更喜欢 OOP...

<?php

class MyDBHandle
{
    public $objDB;

    //Creates the DB Object
    public function Init($strHost, $strUsername, $strPassword, $strDB)
    {
        $this->objDB = new mysqli($strHost, $strUsername, $strPassword, $strDB);

    }

    //SQL Query 1
    public function Dos($strLista, $strJustifica, $strSi)
    {
        $objStatement = $this->objDB->prepare("INSERT INTO dos ('lista', 'justifica', 'si') VALUES (?, ?, ?)");
        $objStatement->bind_param("sss",
                $strLista, 
                $strJustifica, 
                $strSi);

        $objStatement->execute();
        $objStatement->free_result();
    }

    //SQL Query 2
    public function Dos2($strLista)
    {

        $objStatement = $this->objDB->prepare("DELETE FROM dos2 WHERE lista2=?");
        $objStatement->bind_param("s",
                $strLista);

        $objStatement->execute();
        $objStatement->free_result();
    }

    //disconnect from the db
    public function Disconnect()
    {
        $this->objDB->Close();
    }
}

//db variables
$dbHost = "xxx";
$dbUser = "xxx";
$dbPass = "xxx";
$dbDB = "xxx";

//initialize and connect to the DB
$objDB = new MyDBHandle();
$objDB->Init($dbHost, $dbUser, $dbPass, $dbDB);

//here is a moded version of your code to execute the new methods
if(isset($_POST['justifica']) && !empty($_POST['justifica']) &&
isset($_POST['si']) && !empty($_POST['si']) &&
isset($_POST['lista']) && !empty($_POST['lista']))
{
    $objDB->Dos($_POST['lista'], $_POST['justifica'], $_POST['si']);
    $objDB->Dos2($_POST['lista']);
}
$objDB->Disconnect();


?>

关于php - 如何在 PHP 和 MySQL 上使用下拉菜单同时删除和插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23352915/

相关文章:

php - 将页眉和页脚作为模板包含在内不好吗?

mysql right join with group by 问题

mysql - 将多个选择从 selectizeInput 传递到 MySQL 查询

java - Selenium 选择选项 NoSuchElementException

php - 如何生成这种随机曲线?

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

javascript - div类背景颜色变化

mysql - 为什么 LIMIT 运算符不能正常工作?

c# - 母版页值中的下拉列表在新选择后未更新

drop-down-menu - 在 IE8 中显示/隐藏下拉列表中的选定选项不起作用