My SQL always adds 24 click to each access instead of 1

Asked

Viewed 67 times

0

I have a script that counts the visits on my web pages and stores mysql, however, it is adding 24 clicks to each access, instead of 1.

What’s wrong with this code?

Look at the script:

<?php
$idcategoria = 19;
     if (!empty($idcategoria)){
         $guiacomercial_cliques=mysqli_query($con,"Select cliques FROM 
         cliquesguia WHERE categoria =".$idcategoria);
         if (mysqli_num_rows($guiacomercial_cliques) > 0){
              $painel=mysqli_query($con,"UPDATE cliquesguia SET cliques = 
              (cliques + 1) where categoria =".$idcategoria);
         }else{
            $painel=mysqli_query($con,"INSERT INTO cliquesguia (categoria, 
            cliques) VALUES (".$idcategoria.", '1')");
         }
     }
?>
  • 2

    Take advantage that you edited the question and put the database structure, describe how you are executing the code and how you identified that it is added 24.

  • 1

    Let me check my Horns!!! They say the problem is in such a $q=@file_get_contents($ssite); which is in your code :)

1 answer

0

Just try it, without the while:

<?php
     if (!empty($idcategoria)){
         $guiacomercial_cliques=mysqli_query($con,"Select cliques FROM 
         cliquesgc WHERE categoria =".$idcategoria);
         if (mysqli_num_rows($guiacomercial_cliques) > 0){
              $painel=mysqli_query($con,"UPDATE cliquesgc SET cliques = 
              (cliques + 1) where categoria =".$idcategoria);
         }else{
            $painel=mysqli_query($con,"INSERT INTO cliquesgc (categoria, 
            cliques) VALUES (".$idcategoria.", '1')");
         }
     }
?>

Remarks if $idcategoria is an integer, there is no need to use quotes. They are only used for string (text).

I could still get better with the clause ON DUPLICATED KEY UPDATE leaving much lighter and lean code. I’ll leave an example of how it works below:

If category is the Primary key (PK) of your table could do just that:

<?php
     if (!empty($idcategoria)){
            $painel=mysqli_query($con,"INSERT INTO cliquesgc (categoria, 
            cliques) VALUES (".$idcategoria.", '1') ON DUPLICATE KEY UPDATE 
            cliques=(cliques+1);");
     }
?> 

Translate, insert into the table cliquesgc if there is no category informed (which must be the primary key), otherwise if the category already exists (Key for duplicate) some +1 to the click field.

  • Friend. keeps adding 24

  • Can you post the entire code? Change the question and post all the code.

  • This code is integer. The only thing it checks out is the variable $idcategory

  • What can cause it to add 24?

  • If you used what I gave you there is no way to insert 24, since I put 'clicks=clicks+1'; that is the value that is in the field clicks sum +1. Where this code is inside an html?

  • When access the database to see the sum of clicks is added 24 clicks to each visit on the page.

  • I can put this script on any of my pages, and will be added 24 to each access.

  • Category is the primary key of your table?

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.