I need help deleting a row from a mysql database using a drop down menu form.
i want to be able to just select the category i want to delete and then hit the delete button.
For some reason I can not get this to work. I'm also fairly new to PHP.
Here is the code for the form:
<table width="100%" border="0" cellpadding="0" cellspacing="5">
<form name="delete category" method="get" action="removecategory.php">
<tr>
<td> </td>
<td><strong>Select Category:</strong></td>
</tr>
<tr>
<td> </td>
<td >
<?php
include 'config.php';
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = mysql_query("SELECT id,category FROM categories ORDER BY category");
$row = mysql_fetch_array($sql);
?>
<select name="categoryname">
<?php do{ ?>
<option value="<?php echo $row['category']; ?>"><?php echo $row['category']; ?> </option>
<?php } while($row = mysql_fetch_array($sql));?>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input name="submit" type="submit" value="Delete Category"></td>
</tr>
</form>
</table>
Here is the code that the form is submitted too:
<?php
include 'config.php';
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
//check if submit button was pressed
if($_get['categoryname']){
//to protect from mysql injections
$remove=mysql_real_escape_string($_get['categoryname']);
$remove = stripslashes($remove);
$sql = "DELETE FROM categories WHERE `category` = $remove ";
//declare in the sql variable
$result = mysql_query($sql); //order executes
if($result){
{
echo("<br>Category deleted");
}
else{
echo("Failed to Delete category");
}
?>