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!

Recommended Answers

All 39 Replies

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.

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

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!<br>'."\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><br>'."\n";
}
else
{
  echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">'."\n";
  echo '<label for="SiteNumber">Site Number:</label> <input type="text" name="SiteNumber"><br>'."\n";
  echo '<input type="submit" name="submit" value="Delete"><br>'."\n";
  echo '<input type="submit" name="submit" value="Other Action"><br>'."\n";
  echo '</form>'."\n";
}
?>
</body>
</html>
<?
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.

<?
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 a major security vulnerability!)

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

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.

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

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

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

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!<br>'."\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><br>'."\n";
}
else
{
  echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">'."\n";
  echo '<label for="SiteNumber">Site Number:</label> <input type="text" name="SiteNumber"><br>'."\n";
  echo '<input type="submit" name="submit" value="Delete"><br>'."\n";
  echo '<input type="submit" name="submit" value="Other Action"><br>'."\n";
  echo '</form>'."\n";
}
?>
</body>
</html>

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

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?

try it this way:

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

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

try;

mysql_query("DELETE  * FROM UserSites WHERE SiteNumber=".$sn);

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

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!

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

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.

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

Ok, here the the part that gets the data and puts it in the table. Sorry for it being messy, I've messed with it a lot much over the pass couple of days.

mysql_connect(" ", "", "") or die(mysql_error());
mysql_select_db("TACusers") or die(mysql_error());
$result = mysql_query("SELECT * FROM UserSites WHERE Username = '$username' ORDER by Site")
or die(mysql_error());
if(!isset($cmd))
{
	$row_count = 0;
	$columns = 3;
	while ($row = mysql_fetch_assoc($result)) {
	if ($row_count == $columns) {
	echo "</tr><tr>";
	$row_count = 0;
}
	echo "<tr><td>";
	echo "<tr bgcolor=gainsboro><td>";
	echo $row['SiteNumber'];
	$SiteNumber=$row["SiteNumber"];
	echo "</td><td>";
	echo "<a href='".$row["URL"]."'>".$row["Site"]."</a>";   
	echo "</td><td>";
	echo $row['Type'];
	echo "</td><td>";
	echo $row['Description'];
	echo "</td><td>";
	echo "<img src=\"{$row['Rating']}\" alt=\"{$row['Description']}\" />";
	echo "</td><td>";
	echo '<label for="SiteNumber">Site Number:</label> <input type="text" name="SiteNumber"><br>'."\n";
	echo '<input type="submit" name="$SiteNumber" value="Delete"><br>'."\n";
	echo '</form>'."\n";
	echo "</td>";
	$row_count++;
	}
}
echo "</td>";
echo '</table>';

break;

if(isset($_POST['submit']))
{
switch($_POST['submit'])
{

case 'delete':
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post"';
$sn = mysql_real_escape_string($_POST['SiteNumber']);
mysql_query("DELETE * FROM UserSites WHERE SiteNumber=".$sn);
echo 'Site Deleted!<br>'."\n";
echo '</form>'."\n";
}
}
break;

There is more above this, but I don't this it is relevant.

I think I just noticed something, don't I need a Delete case?

Well here you go. I tried to fix a few things and make it a little neater. It pains me to use tables!! Why! That's as bad as forcing me to watch a Yankees game (I'm a Sox fan, obviously...)

<?php
mysql_connect(" ", "", "") or die(mysql_error());
mysql_select_db("TACusers") or die(mysql_error());
$result = mysql_query("SELECT Site, SiteNumber, URL, Description, Type, Rating FROM UserSites WHERE Username='".mysql_real_escape_string($username)."' ORDER by Site");

echo '<table>'."\n".
	 '<tr>'."\n";
if(!isset($cmd))
{
	//Init the row count variable
	$row_count = 0;
	//Print all of the users sites
	while($row = mysql_fetch_assoc($result))
	{
		if ($row_count == 3) 
		{
			echo '</tr>'."\n".'<tr>'."\n";
			$row_count = 0;
		}
		echo '<td>'."\n".
			 '	<table>'."\n".
			 '		<tr bgcolor="gainsboro">'."\n".
			 '			<td>'.$row['SiteNumber'].'</td>'."\n".
			 '			<td><a href="'.$row["URL"].'">'.$row["Site"].'</a></td>'."\n".
			 '			<td>'.$row['Type'].'</td>'."\n".
			 '			<td>'.$row['Description'].'</td>'."\n".
			 '			<td><img src="'.$row['Rating'].'" alt="'.$row['Description'].'" /></td>'."\n".
			 '			<td>'."\n".
			 '				<form action="'.$_SERVER['PHP_SELF'].'" method="post">'."\n".
			 '					<input type="hidden" name="SiteNumber" value="'.$row["SiteNumber"].'">'."\n";
			 '					<input type="submit" name="submit" value="Delete">'."\n".
			 '				</form>'."\n".
			 '			</td>'."\n".
			 '		</tr>'."\n".
			 '	</table>'."\n".
			 '</td>'."\n";
		$row_count++;
	}
}
echo '</tr>'."\n". //Don't forget to end the row
	 '</table>';

//break; <--- What? Why here? I don't see any switches

if(isset($_POST['submit']) && $_POST['submit']=="Delete")
{
	mysql_query("DELETE FROM UserSites WHERE SiteNumber=".mysql_real_escape_string($_POST['SiteNumber']));
	echo '<strong>Site Deleted!</strong><br>'."\n";
	echo '<a href="'.$_SERVER['PHP_SELF'].'"'."\n";
}
//break; <--- What? Again? Why?
?>

It still could use a lot of work...but it should be functional now!

Thank you, that looks so much better, but there is no delete button.

'<input type="submit" name="submit" value="Delete">'."\n".

I wonder what this is then...

I wonder what this is then...

Thanks, but I just changed the type from "hidden" to "submit", and the button still doesn't delete...

The hidden field is an invisible text field. It needs to remain hidden.

<form action="index.php" method="get">
<input type="hidden" name="iam" value="something">
<input type="submit" name="submit" value="See it!">
</form>

If you were to submit the following form, the url would be index.html?iam=something&submit=See It! (<--- The exclamation mark would be URL encoded, but I don't know the code for it). When you view the page though, all you would see is a submit button.

oh ok, I see now. I notice that at the end of each row is a really skinny column. I think that is where the button. I could see and click the button when I changed it from hidden. I really appreciate you helping me, I just don't know why this is so hard.

When you changed it from hidden.....did you not read my post? That is a text field that contains the site number. When you click the delete button, PHP gets the site number and deletes that site from the DB. The input field with the type hidden is not the button! The input that is the submit is the one with the type of submit!

Try widening your table if you can't see the submit button. It's definitely there!

When you changed it from hidden.....did you not read my post? That is a text field that contains the site number. When you click the delete button, PHP gets the site number and deletes that site from the DB. The input field with the type hidden is not the button! The input that is the submit is the one with the type of submit!

Try widening your table if you can't see the submit button. It's definitely there!

I finally got the buttons to show up using the original code that you posted, I just had to put echo in front of it, but still doesn't delete.

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.