Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

How To Delete A Row In Mysql.


5 replies to this topic

#1 apple

    Newbie [Level 2]

  • Kontributors
  • PipPip
  • 32 posts

Posted 28 November 2006 - 07:03 PM

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

}


#2 jlhaslip

    Insert Custom Title Here

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,040 posts
  • Gender:Not Telling
  • Location:Linux, DOS and Windows…the good, the bad and the ugly
  • myCENT:81.07
  • Spam Patrol

Posted 29 November 2006 - 07:28 AM

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

#3 midnitesun

    Premium Member

  • Kontributors
  • PipPipPipPipPipPipPipPip
  • 181 posts

Posted 29 November 2006 - 06:20 PM

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

#4 farsiscript

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 357 posts
  • Interests:... No More !

Posted 31 January 2007 - 07:19 AM

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 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

Edited by farsiscript, 31 January 2007 - 07:22 AM.


#5 QuickSilva

    Premium Member

  • Kontributors
  • PipPipPipPipPipPipPipPip
  • 182 posts
  • Gender:Male
  • Location:Rotherham, UK
  • Interests:I love to help people in any way possible, if it's helping someone with some work, or going to someone's house to fix there computer. I am into Cricket, web design and much more other things. Bye!

Posted 31 January 2007 - 07:25 AM

$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 ;)

#6 Guest_Justin_*

  • Guests

Posted 03 March 2011 - 04:13 AM

View Postjlhaslip, on 29 November 2006 - 07:28 AM, said:

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!




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users