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>&nbsp;</td>
<td><strong>Select Category:</strong></td>
</tr>
<tr>
<td>&nbsp;</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>&nbsp;</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");
} 
?>

Recommended Answers

All 12 Replies

So what do you know is happening? Meaning, at what points in your processing script(code that the form is submitted too) does focus reach? Does this make sense?

I'm not asking for a diagnosis of your problem, I'm trying to see if you have done any debugging yourself yet. Does your script process past your config file? Is this "if($_get){" firing at all. Does the script even get to this point "$result = mysql_query($sql);".

At some point you are going to want to insert "echo $sql . '<br />';" so you can actually look at what is being sent into the database.

You should strategically place echo statements throughout your script, temporarily, so you know these things to help you to isolate the actual issue that is going on.

Does this make sense?

I get an syntax error when it hits the "else' statement.

I get an syntax error when it hits the "else' statement.

You have an odd number of brackets here

if($result){ 
{  
echo("<br>Category deleted");
}
else{
echo("Failed to Delete category");
}

You have an odd number of brackets here

if($result){ 
{  
echo("<br>Category deleted");
}
else{
echo("Failed to Delete category");
}

Okay I've changed the brackets to the following and
now I'm getting the following error.
"parse error: syntax error, unexpected $end in removecategory.php on line 24

18. If($result){
19. echo("<br>Category Deleted");
20. }
21. else{
22. echo("Failed to delete category");
23. }
24. ?>

Okay I've changed the brackets to the following and
now I'm getting the following error.
"parse error: syntax error, unexpected $end in removecategory.php on line 24

18. If($result){
19. echo("<br>Category Deleted");
20. }
21. else{
22. echo("Failed to delete category");
23. }
24. ?>

did you capitalize "If($result)" in your code? I'm pretty sure that won't work. If so, change it to "if($result)"

$_get is suppose to be $_GET

That's all that I see off the top of my head, make those changes and run it again.

still getting the same error on the same line.

still getting the same error on the same line.

Could you post your updated code? I'll test it.

Could you post your updated code? I'll test it.

K here is the up to date code.

<?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");
}
?>

You didn't close the if statement at line 7, this should work:

<?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");
	}
}
?>

okay it parsed the code. But it failed to delete the category. Any ideas??

okay the following changes in the code solved the problem.
In the form /pull down change the code from

<option value="<?php echo $row['category']; ?>">

Change to this:

<option value="<?php echo $row['id']; ?>">

Then change the delete query from

$sql = "DELETE FROM categories WHERE category = $remove";

Change to:

$sql = "DELETE FROM categories WHERE id=$remove ";

Thanks for all your help.

no problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.