将用户视图,国家/地区,城市插入数据库并仅更新视图(如果存在)

时间:2018-02-08 07:39:26

标签: php mysqli

我一直在努力创建一个代码,我可以记录访问过我网站的人以及数据库表中的访问次数。

我的观看次数中有四列。表:

  • IP

  • 城市

  • 国家

  • 城市

现在,新用户访问我的网站,它获取IP地址,城市,县的情况并检查它是否存在于表中,如果IP地址已经存在,则视图将更新为&#39 ; + 1&#39 ;.

如果ip地址不存在,它会将所有数据作为新行插入表中。

但是虽然它获取了IP地址,城市,国家/地区值,但它并没有执行插入查询。

我的代码如下:

<?php
/*Get user ip address*/
$ip_address=$_SERVER['REMOTE_ADDR'];

/*Get user ip address details with geoplugin.net*/
$geopluginURL='http://www.geoplugin.net/php.gp?ip='.$ip_address;
$addrDetailsArr = unserialize(file_get_contents($geopluginURL));

/*Get City name by return array*/
$city = $addrDetailsArr['geoplugin_city'];

/*Get Country name by return array*/
$country = $addrDetailsArr['geoplugin_countryName'];

/*Comment out these line to see all the posible details*/
/*echo '<pre>';
print_r($addrDetailsArr);
die();*/

if(!$city){
   $city='Not Define';
}if(!$country){
   $country='Not Define';
}

$mysqli = new mysqli('localhost', 'username', 'pass', 'dbname');
$result = $mysqli->query("SELECT ip FROM views WHERE ip = '$ip_address'");
if($result->num_rows == 0) {
    $num = '1';

     $sql = "INSERT INTO views (ip, country, city, views)
VALUES ('$ip_address', '$country', '$city', '$num')";

} else {

    $Query = "UPDATE views SET views = views + 1 WHERE ip = '$ip_address' ";
    $RunQuery = mysqli_query ($conn, $Query);
}
?>

请注意,username,pass和dbname将替换为值。

非常感谢任何帮助..

2 个答案:

答案 0 :(得分:0)

您似乎只是在$sql上设置了查询,但没有使用它。

<强>把

$RunQuery = mysqli_query ($conn, $sql); 

<强>后

$sql = "INSERT INTO views (ip, country, city, views)
VALUES ('$ip_address', '$country', '$city', '$num')";

答案 1 :(得分:0)

您未能真正执行insert语句,并且数据库连接对象名称不匹配 - 最初为$mysqli,然后为$conn

<?php

    /*Get user ip address*/
    $ip_address=$_SERVER['REMOTE_ADDR'];

    /*Get user ip address details with geoplugin.net*/
    $url='http://www.geoplugin.net/php.gp?ip='.$ip_address;
    $details = unserialize( file_get_contents( $url ) );

    /*Get City name by return array*/
    $city = $details['geoplugin_city'];

    /*Get Country name by return array*/
    $country = $details['geoplugin_countryName'];




    if( !$city ) $city='Not Defined';
    if( !$country ) $country='Not Defined';


    $db = new mysqli('localhost', 'username', 'pass', 'dbname');

    $result = $db->query( "select `ip` from `views` where `ip` = '$ip_address'" );

    if( $result->num_rows == 0 ) {

        $num = 1;
        $sql = "insert into views ( `ip`, `country`, `city`, `views` ) values ( '$ip_address', '$country', '$city', $num )";
        $result = $db->query( $sql );

    } else {

        $sql = "update `views` set `views` = `views` + 1 where `ip` = '$ip_address' ";
        $result = $db->query( $sql );
    }
?>
$ip_address='72.16.92.178';/* hard-coded ip for testing */

#$ip_address=$_SERVER['REMOTE_ADDR'];

$url='http://www.geoplugin.net/json.gp?ip='.$ip_address;

$details = json_decode( file_get_contents( $url ) );

$city = $details->geoplugin_city;
$country = $details->geoplugin_countryName;

if( !$city ) $city='Not Defined';
if( !$country ) $country='Not Defined';




$dbhost =   'localhost';
$dbuser =   'root'; 
$dbpwd  =   'xxx'; 
$dbname =   'xxx';
$db     =   new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );




$result = $db->query( "select `ip` from `views` where `ip` = '$ip_address'" );

if( $result->num_rows == 0 ) {

    $num = 1;
    $sql = "insert into views ( `ip`, `country`, `city`, `views` ) values ( '$ip_address', '$country', '$city', $num )";
    $result = $db->query( $sql );

} else {

    $sql = "update `views` set `views` = `views` + 1 where `ip` = '$ip_address' ";
    $result = $db->query( $sql );
}


printf( "OK - %s Record Added: %d", $ip_address, $result );



mysql> describe views;
+---------+------------------+------+-----+---------+----------------+
| Field   | Type             | Null | Key | Default | Extra          |
+---------+------------------+------+-----+---------+----------------+
| id      | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| ip      | varchar(64)      | YES  |     | NULL    |                |
| city    | varchar(50)      | YES  |     | NULL    |                |
| country | varchar(50)      | YES  |     | NULL    |                |
| views   | int(10) unsigned | NO   |     | 0       |                |
+---------+------------------+------+-----+---------+----------------+


mysql> select * from views;
+----+--------------+-------------+---------------+-------+
| id | ip           | city        | country       | views |
+----+--------------+-------------+---------------+-------+
|  1 | 88.56.42.78  | Not Defined | Italy         |     5 |
|  2 | 72.16.92.178 | Centerville | United States |     2 |
+----+--------------+-------------+---------------+-------+