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

delete row from mysql using HTML button

I have been working on this a while and have tried just about way trying to delete a row from a database using a button. What I have is a list of websites and data about each on each row. Then next to the row is a Delete button. I just want it to delete that one row and refresh the page when clicked. I already have the table made but when I click the button, it just refreshes the page but nothing happens.
I have tried different things but here is what I have now. I know it is terribly wrong.

echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';

echo '<input type="submit" name="delete" value="Delete ">';
break;

case 'delete';
{
    $sql = "DELETE FROM UserSites WHERE 'SiteNumber'=$SiteNumber";
    $result = mysql_query($sql);
    echo "Site deleted!";
}


I know I need to include SiteNumber somewhere in my button code. Any help would be greatly appreciated!

sfrider0
Junior Poster
149 posts since Oct 2008
Reputation Points: 16
Solved Threads: 0
 
echo '<form action="'.$_SERVER['PHP_SELF'].'"sitenumber="'.$SiteNumber(site number from database).'" method="post">';


.
.
..
..

case 'delete';

{

$sql = "DELETE FROM UserSites WHERE 'SiteNumber'=$_Request[sitenumber]";

$result = mysql_query($sql);

echo "Site deleted!";

}

i am not sure this will work or not . try it once.

rajabhaskar525
Junior Poster
179 posts since Nov 2009
Reputation Points: 12
Solved Threads: 27
 

Thanks, but no luck. Still does the exact same thing.

sfrider0
Junior Poster
149 posts since Oct 2008
Reputation Points: 16
Solved Threads: 0
 

What's between the parenthesis of the switch statement? I think what you are looking for is:

<!doctype html>
<html>
<head>
<title>Input Name Test</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
  switch($_POST['submit'])
  {
    case "Delete":
      mysql_query("DELETE FROM UserSites WHERE SiteNumber=".mysql_real_escape_string($_POST['SiteNumber']);
      echo 'Site Deleted!'."\n";
    break;
    case "Other Action":
      //Other actions can be added with submit buttons with the name submit and a value that corresponds to a case in this switch 
    break;
  }
  echo '<a href="'.$SERVER['PHP_SELF'].'">Go Back</a>'."\n";
}
else
{
  echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">'."\n";
  echo '<label for="SiteNumber">Site Number:</label> <input type="text" name="SiteNumber">'."\n";
  echo '<input type="submit" name="submit" value="Delete">'."\n";
  echo '<input type="submit" name="submit" value="Other Action">'."\n";
  echo '</form>'."\n";
}
?>
</body>
</html>
FlashCreations
Posting Whiz
395 posts since Sep 2008
Reputation Points: 47
Solved Threads: 47
 
<?
ob_start();
extract($_POST);
extract($_REQUEST);
if($_POST['Delete']=='Delete')
{
echo $_REQUEST['sitenumer'];exit;
}
 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
    	 
     <?
	 $a="?sitenumer=raju";
	   echo '<form action="'.$_SERVER['PHP_SELF'].$a.'" method="post" name="formx">';
     
      echo '<input type="submit" name="Delete" value="Delete">';
	  echo '</form>';?>
</body>
</html>


. i am trying to get sitenumber value. i got sitenumber value. modify above code as per your need.

rajabhaskar525
Junior Poster
179 posts since Nov 2009
Reputation Points: 12
Solved Threads: 27
 
<?
ob_start();
extract($_POST);
extract($_REQUEST);
if($_POST['Delete']=='Delete')
{
echo $_REQUEST['sitenumer'];exit;
}
 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
    	 
     <?
	 $a="?sitenumer=raju";
	   echo '<form action="'.$_SERVER['PHP_SELF'].$a.'" method="post" name="formx">';
     
      echo '<input type="submit" name="Delete" value="Delete">';
	  echo '</form>';?>
</body>
</html>
. i am trying to get sitenumber value. i got sitenumber value. modify above code as per your need.

But does it execute the Delete query? Also I believe you meant exit() . And what is the point of the exract() 's? You don't seem to use them anywhere (I would be especially careful with extracting $_GET and $_POST variables as it could pose amajor security vulnerability!)

FlashCreations
Posting Whiz
395 posts since Sep 2008
Reputation Points: 47
Solved Threads: 47
 
echo $_REQUEST['sitenumer'];exit;


comment this one and write delete query definetely excute i am sure about it. if you want know about extract. see this link

rajabhaskar525
Junior Poster
179 posts since Nov 2009
Reputation Points: 12
Solved Threads: 27
 

I fully understand what extract is. Regardless, it poses a security flaw to extract $_GET and $_POST variables. The user would have free roam to set a variable, such as maybe a database to delete, a boolean that determines whether the user is an admin or not, or any other crucial value, to any value including ones that could compromise the script. Extracting $_GET, $_POST, or $_REQUEST is an idea with terrible consequences. I would advise against it.

FlashCreations
Posting Whiz
395 posts since Sep 2008
Reputation Points: 47
Solved Threads: 47
 

thanks for your info. the above code works fine without .

extract($_POST);
extract($_REQUEST);


functions. remove them and see is it works or not.

rajabhaskar525
Junior Poster
179 posts since Nov 2009
Reputation Points: 12
Solved Threads: 27
 

If short tags aren't enabled, then the code won't work. It also doesn't seem that you have mentioned the sitenumber problem in a query. I would still suggest:

<!doctype html>
<html>
<head>
<title>Input Name Test</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
  switch($_POST['submit'])
  {
    case "Delete":
      mysql_query("DELETE FROM UserSites WHERE SiteNumber=".mysql_real_escape_string($_POST['SiteNumber']);
      echo 'Site Deleted!'."\n";
    break;
    case "Other Action":
      //Other actions can be added with submit buttons with the name submit and a value that corresponds to a case in this switch 
    break;
  }
  echo '<a href="'.$SERVER['PHP_SELF'].'">Go Back</a>'."\n";
}
else
{
  echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">'."\n";
  echo '<label for="SiteNumber">Site Number:</label> <input type="text" name="SiteNumber">'."\n";
  echo '<input type="submit" name="submit" value="Delete">'."\n";
  echo '<input type="submit" name="submit" value="Other Action">'."\n";
  echo '</form>'."\n";
}
?>
</body>
</html>
FlashCreations
Posting Whiz
395 posts since Sep 2008
Reputation Points: 47
Solved Threads: 47
 

my code working fine in my server withoutextract() functions. if you pass sitenumber veriable with $_SERVER['PHP_SELF'] . definetely it will working fine.

rajabhaskar525
Junior Poster
179 posts since Nov 2009
Reputation Points: 12
Solved Threads: 27
 

Thanks for all the help, but I'm getting an unexpected ';' in this line

mysql_query("DELETE FROM UserSites WHERE SiteNumber=".mysql_real_escape_string($_POST['SiteNumber']);

Any ideas?

sfrider0
Junior Poster
149 posts since Oct 2008
Reputation Points: 16
Solved Threads: 0
 

try it this way:

$sn = mysql_real_escape_string($_POST['SiteNumber']);
 mysql_query("DELETE FROM UserSites WHERE SiteNumber=".$sn);
JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
 

Thanks JRM. That fixed the error, but still doesn't delete the row.

sfrider0
Junior Poster
149 posts since Oct 2008
Reputation Points: 16
Solved Threads: 0
 

try;

mysql_query("DELETE  * FROM UserSites WHERE SiteNumber=".$sn);
JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
 

*sigh* Read the FAQ at the top of the PHP forum

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Or you could always combine it into one line (It just depends on your coding style):

mysql_query("DELETE FROM UserSites WHERE SiteNumber=".mysql_real_escape_string($_POST['SiteNumber'])); //Added missing parenthesis :)


To diagnose the problem with the row not being deleted, can we see the structure of your database (specifically the UserSites table)? A PhpMyAdmin screenshot would be nice!

FlashCreations
Posting Whiz
395 posts since Sep 2008
Reputation Points: 47
Solved Threads: 47
 

I forgot to make this clear, but this is all assuming that you include a database connection file, or have some code you are not showing to connect to the DB. MySQL isn't just magic...you have to connect to it first:

mysql_connect("HOST", "USERNAME", "PASSWORD") or die("Connection Error!"); //The host is typically localhost, but
                                                                           //it depends on your server setup

mysql_select_db("DATABASE NAME") or die("Database Connection Error!"); //The database in which the table you 
                                                                       //wish to delete a row from is located
FlashCreations
Posting Whiz
395 posts since Sep 2008
Reputation Points: 47
Solved Threads: 47
 

I appreciate all the help. I have already connected to the database and can add stuff to it and have it displaying all the information in it. The delete button is next to each row that is displayed. Everything else is working fine. Must be something else wrong, because I know if shouldn't be this difficult.

sfrider0
Junior Poster
149 posts since Oct 2008
Reputation Points: 16
Solved Threads: 0
 

Can you show your entire code then? It might help to see exactly how we can help with a precise solution...

FlashCreations
Posting Whiz
395 posts since Sep 2008
Reputation Points: 47
Solved Threads: 47
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You