Jump to content
xisto Community
Sign in to follow this  
apple

How To Delete A Row In Mysql. A very simple help required

Recommended Posts

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.

$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 />";

}


Share this post


Link to post
Share on other sites

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 :

<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. :)

Share this post


Link to post
Share on other sites

you can also do the following if jihaslip's code proved to be a little complicated for you


$result = mysql_query("select * from comments order by id desc limit 10");//the while loopwhile($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 :)

Share this post


Link to post
Share on other sites

dear apple you must Get raw id and then use this 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 :
$sql = "DELETE FROM CHANGE_ME_TABEL_NAME WHERE id=$id";$result = mysql_query($sql);
beffor this you must connect to database by this code :
$dbusername = "root"; //write Database username$dbpassword = ""; // write database password$dbname = "mobile";  //write database name// Dont changemysql_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
Edited by farsiscript (see edit history)

Share this post


Link to post
Share on other sites
$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 tableecho $r["comment"];echo "<a href=\"?del=".$r['id']."\">Delete</a><br />";}

I have not tested this ;)

Share this post


Link to post
Share on other sites

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 :

<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. :)

Could you provide an example or where I could find one because I dont quite follow.

Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.