0
votes

I need to create a function which retrieves my data from my database into a drop down list, then choose and remove the selected data. I have my drop down list done & working but I'm experiencing some error with my 'Remove' function.

  <body> 
<?php
//connecting to database
 $server = 'localhost'; 
 $username = 'root'; 
 $password = 'infosys'; 
 $database = 'project'; 
 mysql_connect($server,$username,$password) or die(mysql_error());
 mysql_select_db($database) or die(mysql_error());

//dropdown list to retrieve sentence
$sql = "SELECT words FROM sentence";
$result = mysql_query($sql);

 echo "<select name  
='dropdownlist'>";
while ($row = mysql_fetch_array($result)) {
 echo "<option value='" . $row['words'] ."'>" . $row['words'] ."</option>";
}
echo "</select>";
?>

 <form method="post" action="remove.php"> 
 <input type="submit" value="Remove" /> 
 </form> 
 <a href="index.php" >View list</a>
 </body>

Followed by another php file

<?php

 $server = 'localhost'; 
 $username = 'root'; 
 $password = 'infosys'; 
 $database = 'project'; 
 mysql_connect($server,$username,$password) or die(mysql_error()); 
 mysql_select_db($database) or die(mysql_error());


$dropdownlist1 = $_POST['dropdownlist'];
$dropdownlist2 = $dropdownlist1.value; 
if(isset($_GET['id'])) {
 $mysql_query("DELETE FROM 'words' WHERE id = $dropdownlist2");
 header("location: index.php");
 exit();
}
?>
1
Are you passing id along with the url? Because your delete query works only if $_GET['id'] is set, but form action contains just remove.php - MjZac
In your form, not found your id which you want to pass & what is use of id? - harsh4u

1 Answers

0
votes

Move your Select box or dropdown inside the form tag,

 <form method="post" action="remove.php">    
        <select name ='dropdownlist'>
        <?php while ($row = mysql_fetch_array($result)) { ?>
           <option value='<?php echo $row['words'];?>'><?php echo $row['words'];?></option>
         <?php } ?>
        </select>

    <input type="submit" value="Remove" /> 
 </form> 

in php,

if(isset($_POST['dropdownlist'])) {
      $dropdownlist1 = $_POST['dropdownlist'];
  mysql_query("DELETE FROM `sentence` WHERE `words` = '$dropdownlist1' ");
  header("location: index.php");
  exit();
}

Note: Use mysqli_* or PDO functions instead of using mysql_* functions(deprecated)