PHP/MySQL: UPDATE issue

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
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

PHP/MySQL: UPDATE issue

Post by TonsOfFun »

I'm trying to write a script that will edit a database row but there is a problem in my code I can't seem to find. I'm sure it's obvious, but yet it eludes me.
mysql_error() won't even report anything back

Here the part that I think is causing the problem:

Code: Select all

$query = "UPDATE album_list SET album_name = '$album_name', description = '$desc', caption = '$caption' WHERE album_id = '$album_id'";
		$result = mysql_query($query) OR die (mysql_error());
				
		if(mysql_affected_rows() == 1)	{
						
			echo "<p>You have succesfully updated album <i>$album_name</i>!</p>";
			mysql_close();
			exit();
		} else {
						
			echo mysql_error();
			echo "<p>System Error! Could not update album.<br /> " . mysql_error() . "<br /><br />Query: " . $query . "</p>";
			
			mysql_close();
		}
I can add the full script if needed.

Thanks in advance for your input.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: PHP/MySQL: UPDATE issue

Post by mikosiko »

and what is according to you the problem?....
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

Re: PHP/MySQL: UPDATE issue

Post by TonsOfFun »

mikosiko wrote:and what is according to you the problem?....
That's the thing, I can't figure out a specific problem other than that it simply won't work.

I don't get any errors besides the one I created with the die() statement.
When I run the script, I get:
System Error! Could not update Album 1.


Query: UPDATE album_list SET album_name = 'Album Test', description = 'Test', caption = 'Testing' WHERE album_id = ''
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: PHP/MySQL: UPDATE issue

Post by mikosiko »

so basically your update is not affecting any row, because your WHERE clause is referencing an inexistent row most likely....
TonsOfFun wrote:WHERE album_id = ''
do you have a record with an album_id = '' ??

check the value of $album_id before to construct your $query sentence.
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

Re: PHP/MySQL: UPDATE issue

Post by TonsOfFun »

Of course, simple fix.
I missed created that variable.

Thanks mikosiko.
Post Reply