Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

PHP delete from mysql database


  • Please log in to reply

#1
outatime

outatime

    New Member

  • Member
  • Pip
  • 8 posts
I have a page where my client can delete entries in the database. When the page loads it shows each row of the table with a delete option next to it. The problem is that when you delete one row it deletes ALL the rows. Here is my code:

<? 
$con = mysql_connect("mysql","xxx","xxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
	
mysql_select_db("inventory", $con);

if(!isset($cmd)) 
{

   $result = mysql_query("SELECT * FROM main"); 

   while($r=mysql_fetch_array($result)) 
   { 

	  
	  $text=$r["text"];
	 

	  echo "<a href='delete.php?cmd=delete&text=$text'>$text - Delete</a>";
	  echo "<br>";
	  
	}
}
?>

<?
if($_GET["cmd"]=="delete")
{
	$sql = "DELETE FROM main";
	$result = mysql_query($sql);
	echo "Row deleted!";
}
mysql_close($con);
?>

  • 0

Advertisements


#2
amw_drizz

amw_drizz

    Member

  • Member
  • PipPipPip
  • 329 posts
Pretty simple solution with the code you provided you are telling php to empty the whole table called main. The issue with the way you got it IMO is 2 things, first you are using a column called text. If it is just that text this is a bad way to delete items since if you have a duplicate entry it will also be deleted (something that not everyone may want to happen).

Here is something to consider, if you have an ID tag or a auto-increment primary key use that for the delete code.

<?
$con = mysql_connect("mysql","xxx","xxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
	
mysql_select_db("inventory", $con);

if(!isset($cmd))
{

   $result = mysql_query("SELECT * FROM main");

   while($r=mysql_fetch_array($result))
   {

	  
	  $text=$r["text"];
	  $id=$r['id']; // Use if you have an auto-increment field

	  echo "<a href='delete.php?cmd=delete&text=$id'>$text - Delete</a>";
	  echo "<br>";
	  
	}
}
// Also there is no reason to close and reopen the php tag unless you inputting html directly that will always show.
if($_GET["cmd"]=="delete")
{
	$text = $_GET['text'];
	$sql = "DELETE FROM main WHERE id=$text"; // replace with column name to use as the identifier. ie text this way you only delete that row.
	$result = mysql_query($sql);
	echo "Row deleted!";
}
mysql_close($con);
?>

Edited by amw_drizz, 19 March 2010 - 05:38 AM.

  • 0

#3
outatime

outatime

    New Member

  • Topic Starter
  • Member
  • Pip
  • 8 posts
Thanks! Sorry it took so long to get back. But that's exactly what I needed.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP