Jump to content


Photo

Deleting records


  • Please log in to reply
9 replies to this topic

#1 0riole

0riole
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationAustralia

Posted 04 October 2005 - 05:54 AM


The script below, should from my BASIC knowledge, retrieve the records
stored in the table "venues" display the value "name" along with a link
to modify the record on a separate page (manage_edit.php) or delete the
record and reload this page (manage.php). The problem is that it dose
not delete just reloads. Where have I gone wrong?

Thanks,
Chris

-----------------------------------------------------------------
<?
$usr = "--username--";
$pwd = "--password--";
$db = "--MySQL_Database--";
$host = "localhost";

//Setup Veribales
$id = $_POST['id'];
$name = $_POST['name'];

$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }

if ($task=="del") {
$sql = "DELETE FROM venues WHERE id = '$id'";
mysql_db_query($db, $sql, $cid);

}


?>
<HTML>
<HEAD>
<TITLE>Manage Links</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" >
<H1>Edit Links</H1>
<?

$sql = " SELECT * FROM venues ";
$retid = mysql_db_query($db, $sql, $cid);
if (!$retid) { echo( mysql_error()); }
else {
echo ("<P><TABLE CELLPADDING=4>\n");
while ($row = mysql_fetch_array($retid)) {
$name = $row['name'];
$id = $row['id'];

echo ("<TR>");
echo ("<TD>$name</TD>\n");
echo ("<TD><A HREF=\"manage_edit.php?id=$id
\">Edit</A></TD>");
echo ("<TD><A HREF=\"manage.php?id=$id&task=del
\">Delete</A></TD>");
echo ("</TR>");
}
echo ("</TABLE>");
}
?>

</BODY>
</HTML>


#2 Cook

Cook
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts
  • LocationSingapore

Posted 04 October 2005 - 05:59 AM

You connect to your db server, but you don't select a db to work with. Try adding a mysql_select_db() call right after your mysql_connect() call.

:edit: my bad, didn't see you were using mysql_db_query() rather than mysql_query(), let me have another look at this code then...
Cook

#3 Cook

Cook
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts
  • LocationSingapore

Posted 04 October 2005 - 06:02 AM

Ok, think I got it, try adding this right after your $id and $name init at the beginning of your script:

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]$task = $_GET[\'task\'];[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]
Cook

#4 0riole

0riole
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationAustralia

Posted 04 October 2005 - 06:08 AM

Ok, think I got it, try adding this right after your $id and $name init at the beginning of your script:

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]$task = $_GET[\'task\'];[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

View Post


Tried that, also suggested by a user on an Australian forum, but still no good also had suggested putting in the id and name in that querry, but not shure how to go about this

#5 Cook

Cook
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts
  • LocationSingapore

Posted 04 October 2005 - 06:16 AM

Your $id is also given to your script via the GET method, not the POST method, so change this line:

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]$id = $_POST[\'id\'];[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

to this:

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]$id = $_GET[\'id\'];[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

And this line:

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]$name = $_POST[\'name\'];[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

appears to be pretty useless.
Cook

#6 0riole

0riole
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationAustralia

Posted 04 October 2005 - 06:34 AM

Your $id is also given to your script via the GET method, not the POST method, so change this line:

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]$id = $_POST[\'id\'];[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

to this:

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]$id = $_POST[\'id\'];[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

And this line:

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]$id = $_POST[\'id\'];[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

appears to be pretty useless.

View Post



OK Done all that and still no joy, what else could be going wrong?

#7 Cook

Cook
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts
  • LocationSingapore

Posted 04 October 2005 - 06:35 AM

Suggest you post the current version of your script.
Cook

#8 0riole

0riole
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationAustralia

Posted 04 October 2005 - 06:43 AM

Suggest you post the current version of your script.

View Post

<?
$usr = "username";
$pwd = "password";
$db = "database";
$host = "localhost";

//Setup Veribales
$id = $_GET['id'];
$task = $_GET['task'];

$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }

if ($task=="del") {
$sql = "DELETE FROM venues WHERE id = '$id' name='$name'";
mysql_db_query($db, $sql, $cid);

}


?>
<HTML>
<HEAD>
<TITLE>Manage Links</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" >
<H1>Edit Links</H1>
<?

$sql = " SELECT * FROM venues ";
$retid = mysql_db_query($db, $sql, $cid);
if (!$retid) { echo( mysql_error()); }
else {
echo ("<P><TABLE CELLPADDING=4>\n");
while ($row = mysql_fetch_array($retid)) {
$name = $row['name'];
$id = $row['id'];

echo ("<TR>");
echo ("<TD>$name</TD>\n");
echo ("<TD><A HREF=\"manage_edit.php?id=$id\">Edit</A></TD>");
echo ("<TD><A HREF=\"manage.php?id=$id&task=del\">Delete</A></TD>");
echo ("</TR>");
}
echo ("</TABLE>");
}
?>

</BODY>
</HTML>


#9 Cook

Cook
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts
  • LocationSingapore

Posted 04 October 2005 - 07:05 AM

Now your delete query is not well formed. Do you need the name condition? If so, your need to put an and in between the two tests. But then, where does your $name var come from?

$sql = "DELETE FROM venues WHERE id = '$id' name='$name'";

Cook

#10 0riole

0riole
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationAustralia

Posted 04 October 2005 - 07:24 AM

Now your delete query is not well formed. Do you need the name condition? If so, your need to put an and in between the two tests. But then, where does your $name var come from?

$sql = "DELETE FROM venues WHERE id = '$id' name='$name'";

View Post



Thanks,

All happy now :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users