Strange query

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
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Strange query

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try this:

Code: Select all

UPDATE `table` SET `hits`=`hits`+1 WHERE `activity` = '1' AND `number`='$num'
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post by Archy »

Unfortunatly that did not work.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

have you tried using this query inside phpMyAdmin?
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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:
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post 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" />
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

are register_globals on?
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post by Archy »

How can I turn them on, I am not sure if they are on or off.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post by Archy »

Register Globals are: ON
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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:
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the actual page, being the still with the header call? hmm interesting...
Post Reply