tags:

views:

30

answers:

1

Hi , I have a problem with mysql query ..

I have a table in the data .. name is (Info_website) , the field is (NumVisitor) ..

the problem when the visitor refresh the page I want the filed (NumVisitor + 1) but infact it +2 ..

this is the code ..

$sql2 = "UPDATE `Info_website` SET `NumVisitor` = `NumVisitor`+1 WHERE `Id`=1 LIMIT 1";
$query = mysqli_query($connect, $sql2) or die (mysqli_error());

thank you very much ..

// New Comment

<?php
require("startsession.php");
require("config.php");
include "../include/header.php";
echo '<title>'.$setting['websit_title'].' - Main Page </title>';
?>
<br />
<br />
<table align="center" width="65%">
<tr>
    <td class="head_title" colspan="4">Information</td>
</tr>
<tr>
    <td width="35%">Number of New Book:</td>
    <td>
        <?php
            $sql = "SELECT IDkalemat FROM main_kalemat";
            $query = mysqli_query ($connect , $sql) or die (mysqli_error());
            $num = mysqli_num_rows($query);
            echo $num;
        ?>      
    </td>

    <td width="35%">Number of Users:</td>
    <td>
        <?php
            $sql = "SELECT IDUser FROM user_kalemat";
            $query = mysqli_query ($connect , $sql) or die (mysqli_error());
            $num = mysqli_num_rows($query);
            echo $num;
        ?>
    </td>
</tr>
        <tr>
        <td width="35%">Number Of Visitors:</td>
    <td>
        <?php
            $sql = "SELECT NumVisitor FROM Info_website";
            $query = mysqli_query ($connect , $sql) or die (mysqli_error());
            $num = mysqli_fetch_array($query);
            echo $num['NumVisitor'];
        ?>
    </td>
</tr>

</table>

<br />
<br />
<?php

//-------- from here the number of visitor

//--------- the end
$sql2 = "UPDATE Info_website SET NumVisitor = NumVisitor+1 WHERE Id=1 LIMIT 1"; 
$query = mysqli_query($connect, $sql2) or die (mysqli_error());
if ($query)
{
    echo '1';
}else{
    echo '0';
}
include ("../include/footer.php");
?>
A: 

It sounds like this code is being executed twice on each page visit. Can you show us the rest of the code as well?

David
I added the full code in top of this page ..Thankyou very much ..^_^
AboSami
please what is a problem here . in the code
AboSami
Not sure yet, unless I'm missing something obvious in the code (which would be the same thing you're missing). Can you debug it more on your end? Maybe throw in some echo's and some die's and reload it a few times to see if that single statement is, in fact, incrementing by two. If you narrow it down to just that one statement and take out everything else, does it still increment by two? Re-add other components one at a time until you're back to the original structure and see if that reveals where the second increment is happening.
David
I remove all of query in the page juse query of update the visitor .. afterthat , it was good , it is done ..the query is done .. +1 only ..but I don't know what is the problem ..maybe , when I make more than query in one page it is problem .. thank you David ^_^
AboSami
Sorry , my reputation is 3 only .. I want to vote you .. but I can't .. Thank you for helping me ^_^
AboSami
It's cool, I've hit the reputation cap for today already anyway. Just glad you could get it working :)
David
thank you very much .. ^_^
AboSami