Jump to content

using a link to remove a record from a database????????


son.of.the.morning

Recommended Posts

I have finaly got my removing recods page working, but i do have one problem...

 

i have a delete button which goes to a new php page which contains the DELETE FROM, this deletes the record automaticaly. There are two paths which i could choose from the problem is i am stuck on both of them.

 

1. a link that will activate the delete code.

2. once the record has been deleted then it will automaticaly go back the the first page.

 

can any one help me here please?

The link for deleting the record will be

<a href="delete.php?id=x">Delete</a>

change X to your records id.

 

Now in delete.php you'd do

if(isset($_GET['id']) && is_numeric($_GET['id']))
{
     $id = (int) $_GET['id'];
     
     // dont forget to connect to mysql

     $query = "DELETE FROM table WHERE id=$id";
     mysql_query($query);
}

 

You can redirect a visitor using header. But you can only use the function before your output any text/html.

i cant change the id to x, becuase i am passing a var through it, here is my code

 

 

 

  <?php

$host="------"; // Host name

$username="------"; // Mysql username

$password="------"; // Mysql password

$db_name="--------"; // Database name

$tbl_name="forum_question"; // Table name

 

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

$id=$_GET['id'];

?>

><a href="<? mysql_query("DELETE FROM forum_question WHERE id='$id'"); ?>">yes</a></p>

I didn't say change your ids to x. I meant replace the x with your records id number.

 

<a href="<? mysql_query("DELETE FROM forum_question WHERE id='$id'"); ?>">

You can not pass PHP code within a link. Look at my code example.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.