Forum Moderators: coopster

Message Too Old, No Replies

My script should increment by one when I click a link

but the first time I click it increments by 2!

         

BadGoat

9:54 pm on Dec 11, 2006 (gmt 0)

10+ Year Member



And I am not sure what's wrong :(

This is the script which is executed when the link is clicked:


error_reporting(E_ALL & ~E_NOTICE);

$ip_id = $_GET['ip_id'];

$query = "SELECT * FROM sp_ct WHERE block_id = '$ip_id'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

$query5 = "UPDATE sp_ct SET spct = 1+'".$row['spct']."' WHERE block_id = '$ip_id'" ;
$result5 = mysql_query($query5) or die("<b>mySQL Error:</b>");

if(!$result5)
{
echo 'Error processing request.';
}
else
{
echo '<B><br /><br /><font size="3">Thank you </font></B><br />

'.$row['spct'].'
';
}

echo'

</table>


I am fairly sure that the problem does not lie within this script, as I have tried 3 different queries which would increment the field by one and I get the same results no matter what I try. So I have to think that it is a problem which lies in the script which contains the link, which when clicked leads to the above script. It has to be my noobliness, as there is no reference to '$spct' on this next script.

$spct is set to zero by default.

Logically, I woulda thunk that the above script would be the only one which could increment the variable as it is the only one which refers to that table. The below script pulls variables from a different table, and an ID field in Table A matches another ID field in Table B.

If anyone has an idea of where I am gone wrong I would be most appreciative.

(the other script)


// filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

$x = split("\.",$find);
$find = (256*256*256*$x[0]) + (256*256*$x[1]) + (256*$x[2]) + ($x[3]);

// search for term
$s = long2ip($sip);
$e = long2ip($eip);

echo'

<body>

<!-- MAIN TABLE BEGIN-->
<table align="left" width="98%" border="0" cellpadding="0" cellspacing="0">

<tr>
<td class="C1" width="150px" valign="top">

</td>

<td width="530px" valign="top" bgcolor="white">

<!-- EMBEDDED CENTER TABLE -->

<table width="100%" align="center">

<tr>
<td>
<!-- EMBEDDED CENTER SUB-TABLE -->';
if ($searching =="yes")
{
echo'
<table width="100%"><br /><br /><br />
<tr>
<td width="25%"><b>IP Searched For:</b></td>
<td width="75%">'.long2ip($find).'</td>
</tr>
</table>
<!-- END EMBEDDED CENTER SUB-TABLE -->
</td>
</tr>

<tr>
<td>
<!-- EMBEDDED LOWER SUB-TABLE -->';
//If they did not enter a search term we give them an error
if ($find == "")
{
echo'
<table width="100%">
<tr>
<td><center>You forgot to enter a search term</center></td>
</tr>
</table></td>
</tr>

</table>
<!-- END EMBEDDED CENTER TABLE -->

</td>
</tr>
</table>
<!-- END MAIN TABLE -->

</body>

</html>';
exit;
}
$query = "SELECT ips.sip, ips.eip, ips.nic, ips.updated, ips.company_id, company.company_id, company.company, company.address1, company.address2, company.zip, company.contact, company.phone, company.e1, company.e2, company.e_ver, company.notes, company.city_id, city.city_id, city.city, company.state_id, state.state_id, state.state, company.country_id, country.country_id, country.country FROM ips, company, city, country LEFT JOIN state ON company.state_id = state.state_id WHERE company.country_id = country.country_id AND company.city_id = city.city_id AND ips.company_id = company.company_id AND sip < '$find' AND eip > '$find'"; // exactly like this
$data = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); // explain what's wrong

// display the results
while($result = mysql_fetch_array( $data ))
{
echo "<br>";
}
// counts the number or results
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
$date=date('D M j Y G:i:s');
$sqlquery = "INSERT INTO todo VALUES('','$find', '$date')";
$queryresult = mysql_query($sqlquery) or die(" Could not execute mysql query!");
echo'
<table width="100%">
<tr>
<td><center>The IP Address you entered was not found in our database. </center></td>
</tr>
</table>';
} else {
}
}
$query = "SELECT ips.ip_id, ips.sip, ips.eip, ips.nic, ips.updated, ips.company_id, company.company_id, company.company, company.notes, company.address1, company.address2, company.zip, company.contact, company.phone, company.e1, company.e2, company.e_ver, company.city_id, city.city_id, city.city, company.state_id, state.state_id, state.state, company.country_id, country.country_id, country.country FROM ips, company, city, country LEFT JOIN state ON company.state_id = state.state_id WHERE company.country_id = country.country_id AND company.city_id = city.city_id AND ips.company_id = company.company_id AND sip < '$find' AND eip > '$find'"; // exactly like this
$result = mysql_query($query);
$row = mysql_fetch_array($result);
echo'
<br>
<table width="100%">
<tr>
<td class="selcol1" width="25%"><b>Company:</b></td>
<td class="selcol2" width="75%">'.$row['company'].'</td>
</tr>';

if (!empty($row['address1'])) {

echo '<tr>
<td class="selcol1" valign="top"><b>Address:</b></td>
<td class="selcol2">'.$row['address1'].'</td>
</tr>';

} else {

echo '<tr>
<td class="selcol1" valign="top"><b>Address:</b></td>
<td class="selcol2"></td>
</tr>';

}

if (!empty($row['address2'])) {

echo '<tr>
<td>&nbsp;</td>
<td class="selcol2">'.$row['address2'].'</td>
</tr>';

} else {

}

if (!empty($row['city'])) {

echo '<tr>
<td>&nbsp;</td>
<td class="selcol2">'.$row['city'].'';

} else {

}

if (!empty($row['state'])) {

echo ',&nbsp;'.$row['state'].'';

} else {

}

if (!empty($row['zip'])) {

echo '&nbsp;&nbsp;'.$row['zip'].'</td>
</tr>';

} else {

}

if (!empty($row['country'])) {

echo '<tr>
<td>&nbsp;</td>
<td class="selcol2">'.$row['country'].'</td>
</tr>';

} else {

}

echo'</td>
</tr>

<tr>
<td class="selcol1"><b>Contact:</b></td>
<td class="selcol2">'.$row['contact'].'</td>
</tr>

<tr>
<td class="selcol1"><b>Phone:</b></td>
<td class="selcol2">'.$row['phone'].'</td>
</tr>

<tr>
<td class="selcol1"><b>Email:</b></td>
<td class="selcol2"><b><font color="red">'.$row['e1'].'</font></b></td>
</tr>

<tr>
<td class="selcol1"><b>Email Verified:</b></td>
<td class="selcol2">'.$row['e_ver'].'</td>
</tr>

<tr>
<td colspan="2"><center><br /><a class="style1" href="ip_rec.php?ip_id='.$row['ip_id'].'"> CLICK </a></td>
</tr>

</table>

restless

3:22 am on Dec 12, 2006 (gmt 0)

10+ Year Member



If you only need to increment your link by ONE when the link is clicked then you do not need your first query although it should work anyway. I would check to see what value $row['spct'] is giving by doing an echo $row['spct']

I would also echo your Update query to and then paste this in your mysql administration tool, to see if it actually does anything.

What you can do to increment by one is this:

UPDATE sp_ct SET spct = 1+spct WHERE block_id = '$ip_id'"

Also I just realized in your Update query you have single quotes ' around the $row['spct'] so mysql is treating like a string, you need to remove them. But using my Update query above should work just fine for you then you don't need 2 queries to the DB.

BadGoat

3:42 am on Dec 12, 2006 (gmt 0)

10+ Year Member



Thanks, I'll give it a try, see what happens :)

mikesmith76

9:12 am on Dec 12, 2006 (gmt 0)

10+ Year Member



Not related to your problem but you are using unescaped input data in your db queries, putting you at risk of sql injection. You should really be using mysql_real_escape_string on any data before building your query

BadGoat

3:11 pm on Dec 12, 2006 (gmt 0)

10+ Year Member



Alright, I changed the query as recommended, and I still have the same problem. The update code as it currently is:

$ip_id = $_GET['ip_id'];

$query5 = "UPDATE sp_ct SET spct = 1+spct WHERE block_id = '$ip_id'" ;
$result5 = mysql_query($query5) or die("<b>mySQL Error:</b>");

if(!$result5)
{
echo 'Error processing request.';
}
else
{
echo '<B><br /><br /><font size="3">Thank you </font></B><br />
';
echo $query5;
}

I should note that I went back and clicked the same links as I did yesterday and it incremented by 2 again, but then by only 1 every time after that.. So yesterday's clicks: 2,1,1,1,1
And today's clicks: 2,1,1,1,1

I echoed the query5 and it does show correctly:
UPDATE sp_ct SET spct = 1+spct WHERE block_id = '2121'

(and I will use mysql_real_escape_string once I have this bug worked out! baby steps.. hehe)

BadGoat

4:26 pm on Dec 12, 2006 (gmt 0)

10+ Year Member



The time to edit passed, new thread

I edited the page which contains the link to echo the count.
I opened PHPMyAdmin and checked the count manually.
I clicked the link and looked at the query.
And I was surprised (not really) when the count incremented by two for the first time I clicked the link.

Jordo needs a drink

5:06 pm on Dec 12, 2006 (gmt 0)

10+ Year Member



Do you have adsense running on the page, or anything running in your browser that would cause a hit to the page when you hit the page?

I had a similar problem and discovered the adsense bot was hitting the page right after I hit it causing a multiple hit.

BadGoat

5:12 pm on Dec 12, 2006 (gmt 0)

10+ Year Member



Matter of fact, there is an Adsense script as an include. Is there a workaround for that? (other than not using Adsense?)

Jordo needs a drink

5:24 pm on Dec 12, 2006 (gmt 0)

10+ Year Member




Check the
$_SERVER["HTTP_USER_AGENT"]
before updating spct to make sure it's not the Adsense bot or any other bot you don't want to increase the counter.

Jordo needs a drink

5:27 pm on Dec 12, 2006 (gmt 0)

10+ Year Member



The other thing you probably need to do is to not show the adsense ads to yourself especially while coding and dbugging.

You can do that by not including the adsense code if it's your IP.

BadGoat

5:47 pm on Dec 12, 2006 (gmt 0)

10+ Year Member



Please pardon my noobishness:

What do you mean by:

"Check the $_SERVER["HTTP_USER_AGENT"]"

I've done a bit of Googling for this, and I am not sure how to implement to disallow duplicate link hits.

I tried pasting

echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";

$browser = get_browser(null, true);
print_r($browser);

And it errors out at the $browser line.
Warning: get_browser(): browscap ini directive not set.

Does that refer to a PHP.INI entry?

EDIT

Yep :) It does. Fixig that. After looking at a sample $_SERVER['HTTP_USER_AGENT'] I am not sure how to tell if a bot is incrementing.

[edited by: BadGoat at 5:57 pm (utc) on Dec. 12, 2006]

Jordo needs a drink

4:24 am on Dec 13, 2006 (gmt 0)

10+ Year Member



$_SERVER['HTTP_USER_AGENT'] is a php server variable. You can read more about them here [php.net...]

To check if it's the adsense bot, do something like below...

if($_SERVER['HTTP_USER_AGENT'] != "Mediapartners-Google/2.1"){
Update your counters;
}

BadGoat

1:24 pm on Dec 13, 2006 (gmt 0)

10+ Year Member



Many thanks, that gives me the start I need!