Vasu_candy
Vasu_candy

Reputation: 27

I want to delete a selected item that is in combobox using PhP and that must also reflect in Database

This is the php code to delete.

<?php
$namez = $_POST['vendel'];
$name = mysql_real_escape_string($namez);
mysql_connect("localhost","root",
"") or die("Wrong username or password");
mysql_select_db("vendor") or die( "Unable to select database");
mysql_query("DELETE FROM vendoradd WHERE venname='$name'");
?>

And here is the code where I select from a combobox.

<form action="delete.php" method="post">
                     <select name="vendel">
            <?php
                $con=mysqli_connect("localhost","root","","vendor") or die("Unable to connect");
                $query = mysqli_query($con,"select venname from vendoradd");
                while ($row=$query->fetch_assoc()){
                    echo "<option value=\"vendel1\">".$row['venname']."</option>";
                }
            ?>
            </select>

                <input type="submit" value="Delete" id="del">
            </form>

I am not getting any error, the page is redirecting to delete.php file but when I cross check the database the selected data is not deleted. Any help is appreciated. Thanks in advance

Upvotes: 0

Views: 558

Answers (1)

Well... Its because you're posting a value "vendel1", which means that you're always sending the same value, just for you to know... The name of the select combobox is the index in the $_POST and the value in the option is the value, for example, if you have:

<form action="" method="post">
<select name="foo">
    <option value="1">Stack</option>
    <option value="2">Overflow</option>
</select>
<input type="submit">
</form>
<?php
     echo $_POST['foo'];
?>  

And you select the option "Stack" the output of

$_POST['foo']

will be 1.

So if you re going to post with a select combobox, you'll have to give an unique value to each option, so you can delete the correct one in your database. My recommendation for you is to create an unique ID to each user in your table, its better to compare unique ID's than strings... Why? Because if you have two people with the same name, you'll delete both, but, with an unique ID you'll not have that problem. If you want to delete

<form action="delete.php" method="post">
 <select name="vendel">
        <?php
            $con=mysqli_connect("localhost","root","","vendor") or die("Unable to connect");
            $query = mysqli_query($con,"select TRIM(venname) from vendoradd");
            while ($row=$query->fetch_assoc()){
                echo "<option value=".$row['venname'].">".$row['venname']."</option>";
            }
        ?>
 </select>
 <input type="submit" value="Delete" id="del"></form>

And your delete should be like this:

   <?php

      $namez = $_POST['vendel'];
      $name = mysql_real_escape_string($namez);
      mysql_connect("localhost","root", "") or die("Wrong username or password");
      mysql_select_db("vendor") or die( "Unable to select database");
      mysql_query("DELETE FROM vendoradd WHERE TRIM(venname) = TRIM('$name')");

?>

Upvotes: 1

Related Questions