Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从下拉列表中获取值并在查询中使用_Php_Html_Css - Fatal编程技术网

Php 从下拉列表中获取值并在查询中使用

Php 从下拉列表中获取值并在查询中使用,php,html,css,Php,Html,Css,我在使用php删除数据库中的值时遇到问题,有人能帮我plsss吗这是我的项目 这就是我得到值的地方 你需要在$food周围加上引号 $sqldel="DELETE FROM menu WHERE food = '$food';"; 只需删除;查询后,在变量$food周围加上单引号 sqldel="DELETE FROM menu WHERE food = $food;"; 应该 sqldel="DELETE FROM menu WHERE food = '$food'"; 以下是您的查询

我在使用php删除数据库中的值时遇到问题,有人能帮我plsss吗这是我的项目

这就是我得到值的地方


你需要在$food周围加上引号

$sqldel="DELETE FROM menu WHERE food = '$food';";

只需删除;查询后,在变量$food周围加上单引号

sqldel="DELETE FROM menu WHERE food = $food;";
应该

sqldel="DELETE FROM menu WHERE food = '$food'";

以下是您的查询的修复程序:

    $sqldel = "DELETE FROM menu WHERE food = '".$food."'";

这就是完整代码的外观。希望对你有帮助

<form name="" method="post" action="">
    <select name="fname" id='mySelect' value='Foodname'>
        <option value="">select</option>
        <option value="option1">option1</option>
        <option value="print server, printer">print server, printer</option>
    </select>
    <input type="submit" name="submit" value="submit" />
</form>

<?php
//Check if Form is submitted
if(isset($_POST['submit']))
{
        //Store submitted value in variable
        $food = $_POST['fname'];
        echo $food;

        //Check if the submitted value is blank or not
        if($food=='')
        {     
            //User submitted blank value - so throw an error
            echo"<script>alert('Please Dont Leave any Blanks')</script>";
        }
        else
        {
            /*user selected a valid value in drop down so we are in else part*/
            //This is your database configuration settings
            $servername = "localhost";
            $username = "root";
            $password = "password";
            $dbname = "yourDBName";

            // Create connection - here you are doing database connection
            $conn = new mysqli($servername, $username, $password, $dbname);
            /* Check connection - If you database configuration settings are wrong it will throw an error and stop there itself  and wont execute your further code*/
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            }

            /*Now Check if record exists in db for the selected value. If record exists in database than only we can delete record.*/
            $sql = "SELECT id FROM menu WHERE food = '".$food."'";
            $result = $conn->query($sql);

                /*check if select query return a row greater than 0, implies record exists in table */
                if ($result->num_rows > 0) {              

                        /*Record exists in database so - sql to delete a record*/
                        $delete_sql = "DELETE FROM menu WHERE food = '".$food."' ";
                        /*this will execute the delete query, if it return true we will show success alert else throw an error*/
                        if ($conn->query($delete_sql) === TRUE) {

                            echo"<script>alert('Record deleted successfully')</script>";

                        } else {
                            echo "Error deleting record: " . $conn->error;
                        }          


                } else {
                      echo"<script>alert('No record found for the seleted item')</script>";
                }                    

            //Close database connection
            $conn->close();

        }   

}
?> 

选择
选择1
打印服务器、打印机

语法错误,意外的“=”即使我输入了qoutes与否,这仍会显示语法错误,@Hrishikesh wahmareyah中意外的“=”我使用这个,但没有记录删除:(@LuthandoLoot@jeiidii您正在执行查询字符串吗?类似于
$conn->query(sqldel);
查看我的更新答案。我在每行添加了注释来解释它们