MySQL query not working

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
simcityfreak4
Forum Newbie
Posts: 5
Joined: Fri Jun 22, 2007 4:50 pm

MySQL query not working

Post by simcityfreak4 »

My script has 3 input fields, and when you press submit this is the code it posts to:

Code: Select all

$idu = mysql_real_escape_string($_GET['id']);
$rid = mysql_real_escape_string($_GET['rid']);
$viewa = mysql_real_escape_string($_POST['VIEW_AVS']);
$timed = mysql_real_escape_string($_POST['VIEW_TIME']);
$timefp = mysql_real_escape_string($_POST['timefp']);
$RULEZ = "UPDATE `Members` SET `viewa` = '$viewa', `timed` = '$timed', `timef` = '$timefp' WHERE `board` = '$mboard' AND `user` = '$muser' AND `id` = '$idu'";
echo $RULEZ;
mysql_query($RULEZ);
echo mysql_error();  //Remove when released
When I echo $RULEZ I get this:

Code: Select all

UPDATE `Members` SET `viewa` = '1', `timed` = '1', `timef` = 'asdf' WHERE `board` = 'Brian' AND `user` = 'Brian' AND `id` = '3'
I took the query and ran it in phpMyAdmin and it worked fine, but when I have the script run it it just makes the fields blank.

Any ideas?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

change

Code: Select all

mysql_query($RULES)
to

Code: Select all

mysql_query($RULEZ) or die(mysql_error());
to see why its failing
simcityfreak4
Forum Newbie
Posts: 5
Joined: Fri Jun 22, 2007 4:50 pm

Post by simcityfreak4 »

I changed it and the script doesn't die, it keeps going.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Oh sorry, I didn't realize you had mysql_error() already in there.. try selecting the row before the update, and post the results here just so we can see if the WHERE conditions are being met
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Since you are using an UPDATE query, try using mysql_affected_rows() to see if something was done to the row.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

What does

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', true);

$idu = mysql_real_escape_string($_GET['id']);
$rid = mysql_real_escape_string($_GET['rid']);
$viewa = mysql_real_escape_string($_POST['VIEW_AVS']);
$timed = mysql_real_escape_string($_POST['VIEW_TIME']);
$timefp = mysql_real_escape_string($_POST['timefp']);
$RULEZ = "UPDATE `Members` SET `viewa` = '$viewa', `timed` = '$timed', `timef` = '$timefp' WHERE `board` = '$mboard' AND `user` = '$muser' AND `id` = '$idu'";
echo '<div>Debug: ', $RULEZ, "</div>\n";
mysql_query($RULEZ) or die(mysql_error());

echo '<div>affcted rows: ', mysql_affected_rows(), "</div>\n";

$query = "SELECT `viewa`,`timed`,`timef` FROM `Members` WHERE `board`='$mboard' AND `user`='$muser' AND `id`='$idu'";
$result = mysql_query($query) or die(mysql_error());
echo '<table><tr><th colspan="3">', mysql_num_rows($result), '</th></tr>';
while ( false!==($row=mysql_fetch_array($result, MYSQL_ASSOC)) ) {
	echo '<tr>
		<td>', $row['viewa'], '</td>
		<td>', $row['timed'], '</td>
		<td>', $row['timef'], '</td>
	</tr>';
}
print?
simcityfreak4
Forum Newbie
Posts: 5
Joined: Fri Jun 22, 2007 4:50 pm

Post by simcityfreak4 »

That prints this:


Debug: UPDATE `Members` SET `viewa` = '1', `timed` = '1', `timef` = 'asdf' WHERE `board` = 'Brian' AND `user` = 'Brian' AND `id` = '3'
affcted rows: 1
1
1 1 asdf
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Then... it's working.
simcityfreak4
Forum Newbie
Posts: 5
Joined: Fri Jun 22, 2007 4:50 pm

Post by simcityfreak4 »

Thats weird, I tried the same script in IE and it works fine, but it doesn't want to work right in FF. :banghead:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

How are you checking for form submission? IE and FF send post vars a little differently at times.
simcityfreak4
Forum Newbie
Posts: 5
Joined: Fri Jun 22, 2007 4:50 pm

Post by simcityfreak4 »

I got it fixed now. I just had the script post to a different page instead and had it run that same update query and it works fine now.

Thanks for everyones help!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The page that it is sent to really makes no difference. The same code could be included on the page the form is on, and if posted back to itself, it should work without issue.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Maybe a caching problem.
Post Reply