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

时间:2015-09-30 11:12:20

标签: php html css

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

这是我得到值的地方



<select name="fname" id='mySelect' value='Foodname'>
&#13;
&#13;
&#13;

这就是我提交后想要做的事情

&#13;
&#13;
if(isset($_POST['submit']))
{

        $food = $_POST['fname'];
        echo $food;

        if($food=='')
              {
                echo"<script>alert('Please Dont Leave any Blanks')</script>";
              }
        else
        {
        sqldel="DELETE FROM menu WHERE food = $food;";
        }
}
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:1)

删除;经过查询并将单引号放在变量$ food

之后
sqldel="DELETE FROM menu WHERE food = $food;";

应该

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

这就是你的完整代码应该是这样的。希望它有所帮助!!!

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

        }   

}
?> 

答案 3 :(得分:0)

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

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