-4

i am having trouble in deleteing values from my database using php can someone help me plsss this is for my project

this is where i get the value

<select name="fname" id='mySelect' value='Foodname'>

and this is what i want to do after i submit it

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;";
        }
}
jeiidii
  • 87
  • 1
  • 10
  • [What's the link and I will show you I have no problems deleting all records :P](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – PeeHaa Sep 30 '15 at 11:14

4 Answers4

1

just remove ; after query and put single quote around variable $food

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

shoulb be

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

Here is a fix for your query:

    $sqldel = "DELETE FROM menu WHERE food = '".$food."'";
Luthando Ntsekwa
  • 4,192
  • 6
  • 23
  • 52
1

This is how your complete code should be look like. Hope it helps!!!

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

        }   

}
?> 
Ruprit
  • 733
  • 1
  • 6
  • 23
0

You need to put quotes around $food.

$sqldel="DELETE FROM menu WHERE food = '$food';";
user2182349
  • 9,569
  • 3
  • 29
  • 41