Jump to content

Creating a delete button script????


xcphil

Recommended Posts

Hi All

 

I have finished coding a very simple discussion forum but after going through everything and finding and finding out that everything works I realised I hadn't done a button to delete a line  post from the mysql db.

I can delete a row from the db manually but what I would like to know is how to delete a the row/post which the delete button/script sits next to when you are looking at the website.

Is there a way of associating a comand with a certain row without having to manually type in which row/post you want to delete.

 

Cheers all :)

Link to comment
https://forums.phpfreaks.com/topic/75350-creating-a-delete-button-script/
Share on other sites

Hi,

 

Try to save the below mentioned code in .php file and upload it to your webserver under a secured direcotry so that only you have

access to this script. Now all what you need is to provide the Primary key id and then clik delete button. Also, don't forget the connection related php code and the name of your table and primarky key.

 

<html>
<body>
<form action ="testt.php" name="thisform" method="get">
<INPUT TYPE="text" NAME="name" SIZE="30">
<br><input type="submit" value="click to delete!" onclick="submit();"><br>
<?php
$var = $_REQUEST['name'];
$delete = "delete from exam_tests where id = \"$var\"";
//you have to put the database connection code yourself.
$db = mysql_connect("localhost", "sarabicc_khaled", "khaled");
mysql_select_db("sarabicc_Test", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");
$result =mysql_query($delete);
if ($result ==true)
{ echo "The row has been deleted successfully"; }
else echo "No rows has been deleted";
?>
</form>
</body>
</html>

sorry, i submitted the code with some mistakes . here is the correct code

 

<html>
<body>
<form action ="testt.php" name="thisform" method="get">
<INPUT TYPE="text" NAME="name" SIZE="30">
<br><input type="submit" value="click to delete!" onclick="submit();"><br>
<?php
$var = $_REQUEST['name'];
//enter the table name and primary key name
$delete = "delete from tablename where primarykeyname = \"$var\"";
//you have to put the database connection code yourself.
$db = mysql_connect("", "", "");
mysql_select_db("", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");
$result =mysql_query($delete);
if ($result ==true)
{ echo "The row has been deleted successfully"; }
else echo "No rows has been deleted";
?>
</form>
</body>
</html>

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.