Page 1 of 1

MySQL query not working

Posted: Fri Jun 22, 2007 4:55 pm
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?

Posted: Fri Jun 22, 2007 5:05 pm
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

Posted: Fri Jun 22, 2007 5:10 pm
by simcityfreak4
I changed it and the script doesn't die, it keeps going.

Posted: Fri Jun 22, 2007 5:22 pm
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

Posted: Fri Jun 22, 2007 5:23 pm
by RobertGonzalez
Since you are using an UPDATE query, try using mysql_affected_rows() to see if something was done to the row.

Posted: Fri Jun 22, 2007 5:26 pm
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?

Posted: Fri Jun 22, 2007 5:54 pm
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

Posted: Fri Jun 22, 2007 6:11 pm
by volka
Then... it's working.

Posted: Fri Jun 22, 2007 10:26 pm
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:

Posted: Fri Jun 22, 2007 11:39 pm
by RobertGonzalez
How are you checking for form submission? IE and FF send post vars a little differently at times.

Posted: Sat Jun 23, 2007 9:07 am
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!

Posted: Sat Jun 23, 2007 5:43 pm
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.

Posted: Sat Jun 23, 2007 6:24 pm
by volka
Maybe a caching problem.