Delete from database - problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CanEr
Forum Newbie
Posts: 1
Joined: Fri Jun 30, 2006 4:06 am

Delete from database - problem

Post by CanEr »

Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


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:

Code: Select all

<?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.


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Chenge this..

Code: Select all

$result = mysql_query($sql);
to this...

Code: Select all

$result = mysql_query($sql) or die(mysql_error());
..and see fi there is an error
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Post by thomas777neo »

Does your script echo "Thanks" if you run it, make sure that the if statement is true.

You might have register globals turned off, so also use the $_GET variable in the query.

Otherwise, Pimptastic' comment is the best option
Post Reply