How to make an ip counter using php and mySQL?

-1

What about colleagues, I wanted to make a visit counter for a website (it is not advanced and it is only prototype) in which for every time a new user visits the page the counter increments by 1 and adds the ip of the user to the database, only that I do not know why my code does not work, I hope you can help me. My code is as follows:

   /*plantilla de la pagina*/

<?php

$con_error='Could not connect';

$mysql_host='localhost';
$mysql_user='root';
$mysql_pass='';
$mysql_db='new_database';

/*

/*connect to server / database*/  
$mysqlcon=mysqli_connect($mysql_host, $mysql_user, $mysql_pass) or die($con_error);

/*select database*/
mysqli_select_db($mysqlcon, $mysql_db) or die($con_error);

?>



    /*codigo que aumenta el contador por ip y guarda la ip*/

<?php
    $user_ip=$_SERVER['REMOTE_ADDR'];
    mysqli_query($mysqlcon, "INSERT INTO hit_ip VALUES ($user_ip)");


    function add_ip () {
        global $mysqlcon;
        global $user_ip;

        $query="SELECT ip FROM hit_ip WHERE ip='$user_ip'";
        $query_run=mysqli_query($mysqlcon, $query);
        if (mysqli_num_rows($query_run)==NULL) {
            $ip_query="INSERT INTO hit_ip VALUES ($user_ip)";
            $result_ip=mysqli_query($mysqlcon, $ip_query);
            if ($result_ip) {
                $query_count="UPDATE hit_counter SET value=value+1 WHERE hits='hits'";
                $result_query=mysqli_query($mysqlcon, $query_count);
            } 
        } 
    }


    add_ip(); 

?>

I hope you can help me, thanks in advance:)

    
asked by INME 18.08.2016 в 02:10
source

1 answer

0

Here the problem was that I forgot to put the single quotes in the part:

$ip_query="INSERT INTO hit_ip VALUES ('$user_ip');

and delete the part:

mysqli_query($mysqlcon, "INSERT INTO hit_ip VALUES ($user_ip)");

that had been introduced as a code to manually test if the query performed the desired action

    
answered by 18.08.2016 в 04:44