We help IT Professionals succeed at work.

PHP Delete record from Table needed on code

error77
error77 asked
on
481 Views
Last Modified: 2012-08-13
Hi all,

I have a bit of code that I use to display a list of records from the database.

It works fine but I would like to add a "delete" link to it where I can click on the link that I want to delete and it then gets deleted.

Can someone help please?

Here's the code

Thanks

 
<?php
  try
   {
     $connection = mysql_connect("localhost","username","password");
     mysql_select_db("my-database", $connection);
     $result = mysql_query("SELECT * FROM my-table");
 
     while($row = mysql_fetch_array($result))
       {
         echo $row['date'];
	 echo $row['textarea'];
							
       }
 
         mysql_close($connection);
           }
             catch(Exception $e)
                {
                   echo $e->getMessage();
                }
  ?>

Open in new window

Comment
Watch Question

Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
You have to execute SQL DELETE statement:
http://www.w3schools.com/sql/sql_delete.asp

Author

Commented:
Sorry, but my question wasn't how to delete a record. I'll explain:

My code above shows a list of records.

I need a link on each record that I can click and it will delete that specific record.

Hope this helps.

Thanks
Senior Software Engineer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Could I have some smaple code please?

Thanks

Author

Commented:
I mean sample code. .. Sorry for the typo.
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
Basically, you will do something like that:

HTML:
 
<a href="/delete.php?id=1">Delete</a>

Open in new window


PHP:
 
<?php
// File: DELELTE.PHP
   $id = $_GET['id'];
   $connection = mysql_connect("localhost","username","password");
   mysql_select_db("my-database", $connection);
   mysql_query("DELETE FROM my-table WHERE id = $id");
?>

Open in new window


If you don't want to load delete.php, you will need to use AJAX to execute the page without reloading the page which is recommended in your case.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.