954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Delete Mysql row from drop down menu form. Help Please

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("Category deleted");
}
else{
echo("Failed to Delete category");
} 
?>
AON07
Light Poster
42 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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['categoryname']){" 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 . '
';" 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?

R0bb0b
Posting Shark
998 posts since Jun 2008
Reputation Points: 358
Solved Threads: 89
 

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

AON07
Light Poster
42 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 
I get an syntax error when it hits the "else' statement.


You have an odd number of brackets here

if($result){ 
{  
echo("Category deleted");
}
else{
echo("Failed to Delete category");
}
R0bb0b
Posting Shark
998 posts since Jun 2008
Reputation Points: 358
Solved Threads: 89
 

You have an odd number of brackets here

if($result){ 
{  
echo("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("Category Deleted");
20. }
21. else{
22. echo("Failed to delete category");
23. }
24. ?>
AON07
Light Poster
42 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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("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.

R0bb0b
Posting Shark
998 posts since Jun 2008
Reputation Points: 358
Solved Threads: 89
 

still getting the same error on the same line.

AON07
Light Poster
42 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 
still getting the same error on the same line.


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

R0bb0b
Posting Shark
998 posts since Jun 2008
Reputation Points: 358
Solved Threads: 89
 
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("Category deleted");
}
else{
echo("Failed to Delete category");
}
?>
AON07
Light Poster
42 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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("Category deleted");
	}
	else
	{
		echo("Failed to Delete category");
	}
}
?>
R0bb0b
Posting Shark
998 posts since Jun 2008
Reputation Points: 358
Solved Threads: 89
 

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

AON07
Light Poster
42 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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.

AON07
Light Poster
42 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

no problem

R0bb0b
Posting Shark
998 posts since Jun 2008
Reputation Points: 358
Solved Threads: 89
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You