|
|
|
|
![]() ![]() |
Nov 28 2006, 07:03 PM
Post
#1
|
|
|
Newbie [Level 2] ![]() ![]() Group: Members Posts: 31 Joined: 9-August 06 Member No.: 28,049 |
Im using the following code, for very simple work.. it simply gets and print the comments which are stored in the database.
Now it will show like comment 1. comment 2. comment 3. etc etc i want to add 'delete' option that i can delete any comment. it displays like comment 1. Delete comment 2. Delete comment 3. Delete So that when i click on delete, it simply deletes the comment and print the remaining comments. Its very simple. i hope you understand. QUOTE $result = mysql_query("select * from comments order by id desc limit 10");
//the while loop while($r=mysql_fetch_array($result)) { //getting each variable from the table echo $r["comment"]; echo "<br />"; } |
|
|
|
Nov 29 2006, 07:28 AM
Post
#2
|
|
|
A computer once beat me at chess, but it was no match for me at kick boxing. ![]() Group: [MODERATOR] Posts: 3,874 Joined: 24-July 05 From: In Trouble Again... still? Member No.: 9,787 ![]() |
Change your code to echo a link using the variable named $id as the get query string in the href portion of the anchor tag. You want the anchor tag to look like this in your Browser :
CODE <a href="delete.php?id=1" > 1 </a> Then write a php page named "delete.php" to handle the deleting of the row from the table. It can also be done from a single page if you check for an isset() on the $_GET, but when you are just learning, I suggest a seperate page for processing the delete. By sure to "LIMIT=1" on the DELETE in your mysql query, or else you might do some damage to the table involved. |
|
|
|
Nov 29 2006, 06:20 PM
Post
#3
|
|
|
Premium Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 181 Joined: 22-February 06 Member No.: 19,007 |
you can also do the following if jihaslip's code proved to be a little complicated for you
CODE $result = mysql_query("select * from comments order by id desc limit 10"); //the while loop while($r=mysql_fetch_array($result)) { //getting each variable from the table $id = $r["id"]; $self = $_SERVER['PHP_SELF']; echo $r["comment"]; echo "<br />"; echo "<a href = \"$self?id=$id\">delete</a>"; } if(isset $_GET['id'])) { $id = $_GET['id']; // --------- here code to connect to mysql and select database ----------- $query= mysql_query("delete FROM comment where id =$id")or die(mysql_error()); } if you already dont have a field called id make one and make it primary key. hope this helped you too |
|
|
|
Jan 31 2007, 07:19 AM
Post
#4
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 357 Joined: 8-April 06 Member No.: 21,487 |
dear apple you must Get raw id and then use this code :
CODE $sql = "DELETE FROM file WHERE id=$id"; $result = mysql_query($sql); in this query $sql = "DELETE FROM file WHERE id=$id you must put id number in $id and then write tabel name and replace with file for example you can use this code : CODE $sql = "DELETE FROM CHANGE_ME_TABEL_NAME WHERE id=$id"; $result = mysql_query($sql); beffor this you must connect to database by this code : CODE $dbusername = "root"; //write Database username $dbpassword = ""; // write database password $dbname = "mobile"; //write database name // Dont change mysql_connect("localhost","$dbusername","$dbpassword") or die(mysql_error()); mysql_select_db("$dbname") or die(mysql_error()); for sot and send the id of raw you can send id number by POST method or GET method for more about send id number you can read dear midnitesun post This post has been edited by farsiscript: Jan 31 2007, 07:22 AM |
|
|
|
Jan 31 2007, 07:25 AM
Post
#5
|
|
|
Premium Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 181 Joined: 15-January 07 From: Rotherham, UK Member No.: 37,245 |
CODE $result = mysql_query("select * from comments order by id desc limit 10"); //the while loop $id = $_GET['id']; if (isset($id)) { mysql_query("DELETE FROM comments WHERE id = '$id'"); echo "Comment deleted!"; } while($r=mysql_fetch_array($result)) { //getting each variable from the table echo $r["comment"]; echo "<a href=\"?del=".$r['id']."\">Delete</a><br />"; } I have not tested this |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 24th July 2008 - 06:31 AM |