Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 466
  • Last Modified:

PHP Delete record from Table needed on code

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

0
error77
Asked:
error77
  • 3
  • 3
1 Solution
 
Mohamed AbowardaSoftware EngineerCommented:
You have to execute SQL DELETE statement:
http://www.w3schools.com/sql/sql_delete.asp
0
 
error77Author 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
0
 
Mohamed AbowardaSoftware EngineerCommented:
Add HTML link normally and set the attr. "href" to PHP page, pass the record ID using GET method, then delete the record using SQL DELETE statement,
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
error77Author Commented:
Could I have some smaple code please?

Thanks

0
 
error77Author Commented:
I mean sample code. .. Sorry for the typo.
0
 
Mohamed AbowardaSoftware EngineerCommented:
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now