Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> How To Delete A Row In Mysql., A very simple help required
apple
post 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 />";

}
Go to the top of the page
 
+Quote Post
jlhaslip
post 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 Icon

Group: [MODERATOR]
Posts: 3,874
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



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. biggrin.gif
Go to the top of the page
 
+Quote Post
midnitesun
post 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 laugh.gif
Go to the top of the page
 
+Quote Post
farsiscript
post 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
Go to the top of the page
 
+Quote Post
QuickSilva
post 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 smile.gif
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Problem On Mysql "order By"(5)
  2. Increment A Mysql Column(6)
  3. Can You Add Images Into A Mysql Database?(20)
  4. Creating Profiles In Php/mysql ?(7)
  5. Php Search Engine Script For Mysql Database(11)
  6. Simple Mysql Query Limiting(9)
  7. Some Mysql Basics(4)
  8. Php, Mysql, Apache(4)
  9. Php And Mysql Applications(3)
  10. Getting Php 5 To Work With Mysql(0)
  11. Problem With A Mysql Join(2)
  12. The Artists Tutorials :mysql Basic Commands(0)
  13. What Coding Languages Are Required?(5)
  14. Making Sure They Did Not Leave Any Required Fields Blank(3)
  15. Delete Problem With Id(4)
  1. [mysql]get Id Of Loged In User?(7)
  2. [mysql/php]need Som Basic Help(13)
  3. [php/mysql]id Trouble [resolved](3)
  4. Mysql Won't Update(4)
  5. Php + Mysql Question!(4)
  6. Script Help Required: Undefined Variable(3)
  7. Tools Needed!(9)
  8. Best Sites For Learning Php-mysql(4)
  9. Php And Mysql Programming(2)
  10. Best Php And Mysql Editor For Noobs(6)
  11. Html Form!(4)
  12. Mysql Error(3)
  13. Create Table - Mysql Code - Help(1)


 



- Lo-Fi Version Time is now: 24th July 2008 - 06:31 AM