Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
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