Jump to content

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given i


mdfcows

Recommended Posts

Hi, I'm fairly new to PHP and have run into this problem, I have tried looking through some other peoples solutions to this but don't quite understand them!

 

I have set up a simple news post on a website which works fine, however when I go to edit a post and try to submit it again it comes up with this error:

 

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /Users/mdfcows/Sites/atelier/editnews.php on line 61

 

It still works, i.e. posts it, however I don't quite know what this means, and now I am trying to set it up so that another page can be edited and all I am getting is the same error on the page without any of the other info or PHP coming up!

 

this is the PHP I am using, with the line

"while ($row = mysql_fetch_assoc ($result)) {"

being line number 61

 

 

<?php
			require_once('config.php');

			$con = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
			if (!$con){
				die('Failed to connect to server' . mysql_error());
			}

			mysql_select_db(DB_DATABASE);

			$ide = $_POST[idf];
			$query = "SELECT image,id,title,text FROM news WHERE id = $ide";
			$result = mysql_query($query); 

			while ($row = mysql_fetch_assoc ($result)) {
				$title = htmlentities ($row['title']);
				$news = nl2br (strip_tags ($row ['text'], '<a><b><i><u>'));
				$image = $row['image'];

				echo "<form class ='addform' action='editnews.php' enctype='multipart/form-data' method='post'>";
				echo "<input type='hidden' name='ida' value='$ide' />";
				echo "<p>Title:<br /><input class='titlefield' type='text' name='title' value='$title' /></p><br />";
				echo "<p>News Post:<br /> <textarea name='news' rows='12' cols='50'>$news</textarea></p><br />";
				echo "<p>If you uploaded an image with your post you will need to upload it again<br /><input type='hidden' name='MAX_FILE_SIZE' value='20000000' /> <input name='userfile' type='file' value='$image' id='userfile' /></p><br />";
				echo "<p><input name='submit' type='submit' value='Submit' /></p>";
				echo "</form>";
			}

			if ($_POST['submit'])
			{
			  
			  mysql_select_db(DB_DATABASE);
			  
			  $upid = $_POST[ida];
			  $uptitle = $_POST[title];
			  $upnews = $_POST[news];
			  $upimage = $_FILES['userfile']['name'];
			  
			  $sql = "UPDATE news SET title = '$uptitle', text = '$upnews', image = '$upimage' WHERE id = '$upid'";
			  
			  mysql_query($sql);
			  
			  if ($_POST['submit'])
			  {
				  
				  echo "<p class='admintext'>Your post has been Edited - <a href='index.php'>View Latest News Page</a></p><br />";
				  $name = $_FILES['userfile']['name'];
				  $type = $_FILES['userfile']['type'];
				  $size = $_FILES['userfile']['size'];
				  $tmpname = $_FILES['userfile']['tmp_name'];
				  $ext = substr($name, strrpos($name, '.'));
				  
				  if (strstr($type, "image"))
				  {
					  move_uploaded_file($tmpname, "images/".$name);
				  }
				  
			  }
			}
				  
			?>

 

 

Any help would be much appreciated!

 

Thank you

 

Martin

Link to comment
Share on other sites

Ok, so I changed that and it now gives me the error:

 

Query:

SELECT image,id,title,text FROM news WHERE id =

Error:

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 '' at line 1

 

I have tried changing the =$_POST[idf];

 

to =$_POST[id];

and =$_POST['id'];

 

but these just make it so that I can't even get to edit the post before the error comes up now!

 

 

Link to comment
Share on other sites

I have tried changing the =$_POST[idf];

 

to =$_POST[id];

and =$_POST['id'];

 

Debugging code does not mean randomly changing logic in the hopes that it will magically solve the problem. What is the field name that holds the id of the value you are trying to get? You have to test/validate what is/in not set and what the values are. Do a print_r($_POST) to validate what is being passed in the post data.

 

I do see int he code above you are trying to populate that vallue into a hidden field

echo "<input type='hidden' name='ida' value='$ide' />";

 

You named that field "ida".

Link to comment
Share on other sites

Ah that's brilliant! it was because that and another part were ida when they needed to be idf!

 

Thanks a lot for your help, has put my mind to rest quite a bit now!

 

I don't quite know how to close this as fixed now either.

Any idea?

 

Thanks again

 

Martin

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.