xcphil Posted October 30, 2007 Share Posted October 30, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/75350-creating-a-delete-button-script/ Share on other sites More sharing options...
aschk Posted October 30, 2007 Share Posted October 30, 2007 Are your rows uniquely identified in your database (i.e. auto_inc id) ? Then delete by id. If not you will have to find another primary key to go by in order to delete them. Quote Link to comment https://forums.phpfreaks.com/topic/75350-creating-a-delete-button-script/#findComment-381084 Share on other sites More sharing options...
khalidorama Posted October 30, 2007 Share Posted October 30, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/75350-creating-a-delete-button-script/#findComment-381138 Share on other sites More sharing options...
khalidorama Posted October 30, 2007 Share Posted October 30, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/75350-creating-a-delete-button-script/#findComment-381145 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.