PDA

View Full Version : Delete from database - problem


HakonHoy
06-30-06, 05:01 AM
I have a problem when I try to delete a record from a table in my MySql db. I can update, add records to the same table. But when I try to delete, nothing happen, no errors. The stuff that happens are the same as should happen if it was deleted, but it doesn't get deleted. I have tried the php code on another server, and there it works as it should and deletes the stuff from the db. Could there be a permission thing with MySql, that says I can't delete records? Or something like that? Is there a way to check that? Like e.g. php info?

Php Code:
<?php

//connect to MySQL

$db="dbname";

$connect = mysql_connect('host', 'user', 'pass');
if (! $connect)
die("Couldn't connect to MySQL");
mysql_select_db($db , $connect) or die("Select Error: ".mysql_error());

$query = "SELECT * FROM newscat ORDER BY categn DESC LIMIT 0, 25";
$results = mysql_query($query)
or die(mysql_error());

while ($row = mysql_fetch_array($results)) {
extract($row);
echo "<a href=\"editcategory2.php?id=$newscat_id\" onclick=\"return confirm('Are you realy sure you want to edit this category?')\">Edit Category</a> - ";
echo "<b>$categn</b>";
echo " - <a href=\"editcategory.php?newscat_id2=$newscat_id&delete=1\" onclick=\"return confirm('Are you realy sure you want to delete this category?')\">Delete Category</a> ";
echo "<br>";

}

if($_GET['newscat_id2'] && $_GET['delete'] == "1") {

$sql = "DELETE FROM newscat WHERE newscat_id = $newscat_id2";

$result = mysql_query($sql);

echo "Thanks.";
}


?>

I would be very happy if you could help me with this.

Nico
06-30-06, 05:14 AM
Moved to database.


Do you have register globals on?

If not you will have to change this


$sql = "DELETE FROM newscat WHERE newscat_id = $newscat_id2";


To this


$sql = "DELETE FROM newscat WHERE newscat_id = ". $_GET['newscat_id2'];

HakonHoy
06-30-06, 07:15 AM
It worked, great :) Thanks.

on more thing... I got this error, in another file that worked fine on the other server: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '= cmtnews_id AND cmt_checked = '1' ORDER BY cmt_date DESC' at l

Here is the code:

$sql = "SELECT * FROM cmt_table WHERE $news_id = cmtnews_id AND cmt_checked = '1' ORDER BY cmt_date DESC";
$results3 = mysql_query($sql)
or die(mysql_error());
echo "<table width=100% height=1 cellspacing=0 cellpadding=2 vspace=0 hspace=0><tr><td valign=top width=100% bgcolor=#990000 height=1><font face=\"Verdana,Arial\" size=\"1\" color=\"#ffffff\"><b>COMMENTS TO THIS NEWS</b></font></td></tr></table><table width=100% height=1 cellspacing=0 cellpadding=2 vspace=0 hspace=0><tr><td valign=top width=100% bgcolor=#EEEEEE>";
while ($row3 = mysql_fetch_array($results3)) {
extract($row3);
echo "<font face=\"Verdana,Arial\" size=\"1\" color=\"#000000\"><b>$cmt_poster, $cmt_country</b><br>$cmt_txt</font><br><br>";
}

Why do I get a error here then? Is it the same? U know?

Nico
06-30-06, 07:28 AM
Try changing the order here:

Change this

$news_id = cmtnews_id


To this

cmtnews_id = $news_id

HakonHoy
06-30-06, 07:36 AM
It helped a bit:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND cmt_checked = '1' ORDER BY cmt_date DESC' at line 1

Do you see other mistakes I have made?

Nico
06-30-06, 08:15 AM
Hm, try placing single quotes around $news_id


cmtnews_id = '$news_id'