Page 1 of 1

Strange query

Posted: Mon Jan 17, 2005 5:49 am
by Archy
I have a query that updates a database when a user displays a banner on his/her site. Below is the code I am using.

Code: Select all

<?PHP
header("Content-type: image/gif");
include('connectToDatabase.php'); // Connect to database

$sql = "UPDATE `table` SET `hits`=`hits`+1 WHERE `activity`=1 && `number`='$num'";
$rs = mysql_query($sql) or die(mysql_error());

readfile("http://www.site.com/image.gif");
?>
However, it seems to disregard && `activity`=1, and so it updates the database even when activity does not equal 1.

Does anybody know if this is because of the content type, and a way around it?

Thanks.

Posted: Mon Jan 17, 2005 7:43 am
by feyd
try this:

Code: Select all

UPDATE `table` SET `hits`=`hits`+1 WHERE `activity` = '1' AND `number`='$num'

Posted: Mon Jan 17, 2005 8:16 am
by Archy
Unfortunatly that did not work.

Posted: Mon Jan 17, 2005 8:20 am
by feyd
have you tried using this query inside phpMyAdmin?

Posted: Mon Jan 17, 2005 8:38 am
by Archy
Yes, the query works fine using phpMyAdmin.

Affected rows: 1 (Query took 0.0012 sec)
SQL-query:
UPDATE `table` SET `hits` = `hits` +1 WHERE `activity` = '1' AND `number` = '406762652'

I first changed the `activity` to 0, and it did not update, and then I changed it to 1, and it updated successfully; it isnt the query that is wrong, and is why I thought it may have something to do with the content type of the page.

I added the content-type: text/html, and it still did not work.

Posted: Mon Jan 17, 2005 8:43 am
by feyd
hmmm :? What about $num, are you sure that's coming in right? Because it looks like it requires register_globals to be on..

That's about the only thing left outstanding that I can see.. :cry:

Posted: Mon Jan 17, 2005 8:46 am
by Archy
Well, at the moment, $num is being called by the URL.

Example would be:

<img src="http://www.mySite.com/page.php?num=23456789" />

Posted: Mon Jan 17, 2005 8:47 am
by feyd
are register_globals on?

Posted: Mon Jan 17, 2005 8:53 am
by Archy
How can I turn them on, I am not sure if they are on or off.

Posted: Mon Jan 17, 2005 9:05 am
by feyd
you can check via this:

Code: Select all

echo 'Register Globals are: ' . (ini_get('register_globals') == 1 ? '<b>ON</b>' : 'off');
It's better to avoid needing them on, since they are off by default these days like so:

Code: Select all

$num = (isset($_GET&#1111;'num']) && is_numeric($_GET&#1111;'num']) ? intval($_GET&#1111;'num']) : false);

if($num === false) die('you smell like poo');

// do your query

Posted: Mon Jan 17, 2005 9:08 am
by Archy
Register Globals are: ON

Posted: Mon Jan 17, 2005 9:17 am
by feyd
hmm.. and you echo'd the query string to see that $num is getting in there okay?

I'm stumped now.. :lol: :cry: :oops:

Posted: Mon Jan 17, 2005 9:26 am
by Archy
Hmm, if I go to the actual page which updates the code, it works correctly, it just doesnt seem to like going through the <img> tag.

Posted: Mon Jan 17, 2005 9:28 am
by feyd
the actual page, being the still with the header call? hmm interesting...