Jump to content


PHP wont run SQL query


1 reply to this topic

#1 Hit3k

    Young Padawan

  • Members
  • Pip
  • 120 posts
  • Gender:Male
  • Location:Australia

Posted 21 March 2006 - 08:49 PM

Ok.. In the CMS I'm coding the SQL query wont delete anything the code is

<?php
		$dbh=mysql_connect ("localhost", "******", "******") or die ('I cannot connect to the database because: ' . mysql_error());
		mysql_select_db ("news_cms");
		$id = $_POST['delete'];
		if (isset ($_POST['deleted'])) {
			echo $id;
			$query = "DELETE FROM newscms WHERE `news_id` = {$id}";
			if ($r = mysql_query($query)) {
			echo 'The Post Has Been Deleted Successfully';
			}
			else { die('Couldnt Update Data Becase ' . mysql_error() . '');  }
		} 
	?>
and it doesnt delete anything can anyone help me?

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 21 March 2006 - 10:24 PM

Well, you got step one down by echoing the id... is that actually displaying a valid id that is in your database under the news_id column (or since it's in an if-statement, anything at all)?

If so, try using the following for your query:
mysql_query("DELETE FROM newscms WHERE `news_id` = ".$id) or die('Couldn\'t Update Data Because ' . mysql_error());
echo 'The Post Has Been Deleted Successfully';
Keep in mind, what i did with the $id variable really isn't anything, just a matter of preference. I also fixed "Becase" :D

And, just for future reference:
if ($r = mysql_query($query)) {
INSERT, DELETE, and maybe one or two other statements don't need to equal anything. Assigning them to a variable is just a waste if you're using the if-statement like that, or the "or die()" method.

Edited by rc69, 21 March 2006 - 10:24 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users