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

Php 将用户视图、国家/地区、城市插入数据库,并仅更新视图(如果存在),php,mysqli,Php,Mysqli,我一直在尝试制作一个代码,在这个代码中,我可以记录谁访问过我的站点,以及在DB表中访问了多少次 我的“视图”表中有四列: 知识产权 城市 国家 城市 现在,新用户访问我的站点,它获取IP地址、城市、县的情况,并检查它是否存在于表中,如果IP地址已经存在,只需将视图更新为“+1” 如果ip地址不存在,它会将所有数据作为新行插入表中 但是,尽管它正在获取IPaddress、city、country值,但它没有执行插入查询 我的代码如下: <?php /*Get user ip addres

我一直在尝试制作一个代码,在这个代码中,我可以记录谁访问过我的站点,以及在DB表中访问了多少次

我的“视图”表中有四列:

  • 知识产权

  • 城市

  • 国家

  • 城市

现在,新用户访问我的站点,它获取IP地址、城市、县的情况,并检查它是否存在于表中,如果IP地址已经存在,只需将视图更新为“+1”

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

但是,尽管它正在获取IPaddress、city、country值,但它没有执行插入查询

我的代码如下:

<?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);
}
?>

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

put

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

<?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 );
    }
?>

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

$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 |
+----+--------------+-------------+---------------+-------+

您从未执行要插入的第一个查询(
$sql=“insert INTO views…
)我尝试了您的代码,但不起作用。另一方面,我有一个定义了$conn的数据库连接文件。php错误日志中是否有与此相关的错误?我刚刚创建了一个测试表并运行了上述代码(使用我的db connection对象)如果($conn->query($sql)==TRUE){echo“新记录创建成功”;}
$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 |
+----+--------------+-------------+---------------+-------+