...

View Full Version : How do i edit and delete records from database?



thilss0o
03-11-2010, 02:13 AM
Im running an online march madness pool, and i want the users to be able to edit their bracket or delete it entirely.

I already have the submit bracket and view bracket thing down. i just need for them to edit what has already been submitted to the database.

i have read some tutorials but i still havent been able to get it to work. any guidance or code of working examples?

thanks

mlseim
03-11-2010, 02:19 AM
How about showing us some of the code you have.
X-out any passwords you have for your MySQL.

thilss0o
03-11-2010, 02:33 AM
<html>
<body>
<?php
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","xxxx");

//select which database you want to edit
mysql_select_db("ipool");

//If cmd is not hit
if(!isset($cmd))
{
//display all the news
$result = mysql_query("select * from news");

//run the while loop that grabs all the news scripts
while($r=mysql_fetch_array($result))
{
//grab the title and the ID of the enws
$title=$r["title"];//take out the title
$id=$r["id"];//take out the id

echo "<a href='delete.php?cmd=delete&id=$id'>$title - Delete</a>";
echo "<br>";
}
}

if($cmd=="delete")
{
$sql = "DELETE FROM news WHERE id=$id";
$result = mysql_query($sql);
echo "Row deleted!";
}
?>


</body>
</html>


thats what i have for deleting, which i cant get to work either. it shows the list and goes to the automated url correctly, but it wont actually delete the row.
im using the same cmd==update to edit the rows as well and thats getting me just as far as the delete page is

mlseim
03-11-2010, 03:19 AM
Missing the $_GET for getting the variable from the URL ...

Try this:


<html>
<body>
<?php
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","xxxx");

//select which database you want to edit
mysql_select_db("ipool");

// GET variable(s) from your URL.
$cmd=$_GET['cmd'];
$id=$_GET['id'];

//If cmd is not hit
if(!isset($cmd))
{
//display all the news
$result = mysql_query("select * from news");

//run the while loop that grabs all the news scripts
while($r=mysql_fetch_array($result))
{
//grab the title and the ID of the enws
$title=$r["title"];//take out the title
$xid=$r["id"];//take out the id, use $xid because $id is used for another purpose.

echo "<a href='delete.php?cmd=delete&id=$xid'>$title - Delete</a>";
echo "<br>";
}
}

if($cmd=="delete")
{
// Always sanitize any variables you use in your query - to prevent SQL injections.
$id = mysql_real_escape_string($id);

$sql = "DELETE FROM news WHERE id=$id";
$result = mysql_query($sql);
echo "Row deleted!";
}
?>


</body>
</html>

thilss0o
03-11-2010, 03:31 AM
yea ino, i was just about to post to say i got it working now

thanks tho

mlseim
03-11-2010, 03:32 AM
Make sure you do this to any variables in your query ...

// Always sanitize any variables you use in your query - to prevent SQL injections.
$id = mysql_real_escape_string($id);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum