elrando
10-26-2009, 03:40 PM
Hello, i found this snippit i've been trying to fix to delete links i put on my site. (I am working on an admin section) I want it to delete by id specifically, it does delete but it deletes in order the links were put in the database.
Here's the code for delete.php (lists all the links)
<?php
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","");
//select which database you want to edit
mysql_select_db("links");
//display all the links
$result = mysql_query("select * from links order by id");
//run the while loop that grabs all links
while($row=mysql_fetch_array($result))
{
//grab the title and the ID of the news
$id = $row['id'];
$title = $row['title'];
$url = $row['url'];
//make the title a link
echo "<a target=_blank href='$url'>$title</a> <a href='delete_now.php?cmd=delete&id=$id'>Delete</a>";
echo "<br>";
}
?>
Here's the code for delete_now.php (deletes the links from the database)
<?php
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","");
//select which database you want to edit
mysql_select_db("links");
//If cmd has not been initialized
if(!isset($cmd))
{
//display all the links
$result = mysql_query("select * from links order by id");
//run the while loop that grabs all links
while($row=mysql_fetch_array($result))
{
//grab the title and the ID of the news
$id = $row['id'];
$title = $row['title'];
$url = $row['url'];
}
}
if($_GET["cmd"]=="delete")
{
$sql = "DELETE FROM links WHERE id=$id";
$result = mysql_query($sql);
header("location: delete.php");
//echo "Row deleted!";
}
?>
I am new to php any help would be appreciated.
Thanks,
Randy.
Here's the code for delete.php (lists all the links)
<?php
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","");
//select which database you want to edit
mysql_select_db("links");
//display all the links
$result = mysql_query("select * from links order by id");
//run the while loop that grabs all links
while($row=mysql_fetch_array($result))
{
//grab the title and the ID of the news
$id = $row['id'];
$title = $row['title'];
$url = $row['url'];
//make the title a link
echo "<a target=_blank href='$url'>$title</a> <a href='delete_now.php?cmd=delete&id=$id'>Delete</a>";
echo "<br>";
}
?>
Here's the code for delete_now.php (deletes the links from the database)
<?php
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","");
//select which database you want to edit
mysql_select_db("links");
//If cmd has not been initialized
if(!isset($cmd))
{
//display all the links
$result = mysql_query("select * from links order by id");
//run the while loop that grabs all links
while($row=mysql_fetch_array($result))
{
//grab the title and the ID of the news
$id = $row['id'];
$title = $row['title'];
$url = $row['url'];
}
}
if($_GET["cmd"]=="delete")
{
$sql = "DELETE FROM links WHERE id=$id";
$result = mysql_query($sql);
header("location: delete.php");
//echo "Row deleted!";
}
?>
I am new to php any help would be appreciated.
Thanks,
Randy.