Click to See Complete Forum and Search --> : insert into database?


evania
11-24-2007, 09:37 AM
hello.

Im new at php and wonder if someone can help me to insert someting into my database.

what i need help with

how can i make it so that when a users visits my site, their IP address is inserted into the table "stats" under "ip" and the row "views is updated to 1, then if that ip visists the page again the row "views" for that ip is again updated by 1 so that it should say that it visited 2 times.

hope someone can help.

NogDog
11-24-2007, 10:17 AM
Make the IP address field the primary key or a unique index for that table. Then when you do the insert, use the ON DUPLICATE KEY UPDATE syntax:

// assume $ip_address contains the user's IP
$sql = "INSERT INTO `stats` (`ip`, `views`) VALUES ('$ip_address', 1)
ON DUPLICATE KEY UPDATE `views` = `views` + 1";

evania
11-24-2007, 12:37 PM
Make the IP address field the primary key or a unique index for that table. Then when you do the insert, use the ON DUPLICATE KEY UPDATE syntax:

// assume $ip_address contains the user's IP
$sql = "INSERT INTO `stats` (`ip`, `views`) VALUES ('$ip_address', 1)
ON DUPLICATE KEY UPDATE `views` = `views` + 1";


i think i forgot to mention. how would i get the ip address from the user? the query above only inserts an IP if i have it collected i assume? how to i grab the ip of the user then?

h3r0
11-24-2007, 08:11 PM
Use this code to get the user's IP address:

$ip_address = $_SERVER['REMOTE_ADDR'];

evania
11-25-2007, 03:11 PM
Use this code to get the user's IP address:

$ip_address = $_SERVER['REMOTE_ADDR'];


this is how it looks atm, is this enouff to get the up address into the database?

<?
include('other/config.php');

mysql_connect($host, $dbname, $dbpass) or die(mysql_error());
mysql_select_db($dbuser) or die(mysql_error());

$ip_address = $_SERVER['REMOTE_ADDR'];

"INSERT INTO `stats` (`ip`, `views`) VALUES ('$ip_address', 1);
ON DUPLICATE KEY UPDATE `views` = `views` + 1";

ecco mysql_error()

?>

h3r0
11-25-2007, 03:54 PM
Yeah looks like it. Test it out see if it works. Learn by experience.

evania
11-25-2007, 06:18 PM
Yeah looks like it. Test it out see if it works. Learn by experience.

i get no error when i run it but also no entry is added to the database! :(

mitchell
11-25-2007, 06:25 PM
try this:

<?
include('other/config.php');

mysql_connect($host, $dbname, $dbpass) or die(mysql_error());
mysql_select_db($dbuser) or die(mysql_error());

$ip_address = $_SERVER['REMOTE_ADDR'];

$process = mysql_query("INSERT INTO `stats` (`ip`, `views`) VALUES ('$ip_address', 1);
ON DUPLICATE KEY UPDATE `views` = `views` + 1");
or die(mysql_error());

?>

evania
11-25-2007, 09:23 PM
try this:

<?
include('other/config.php');

mysql_connect($host, $dbname, $dbpass) or die(mysql_error());
mysql_select_db($dbuser) or die(mysql_error());

$ip_address = $_SERVER['REMOTE_ADDR'];

$process = mysql_query("INSERT INTO `stats` (`ip`, `views`) VALUES ('$ip_address', 1);
ON DUPLICATE KEY UPDATE `views` = `views` + 1");
or die(mysql_error());

?>

parse error: syntax error, unexpected T_LOGICAL_OR in /home/onlycom/public_html/test.php on line 11

:/

roscor
11-26-2007, 08:02 AM
not $process = mysql_query("INSERT INTO `stats` (`ip`, `views`) VALUES ('$ip_address', 1);
ON DUPLICATE KEY UPDATE `views` = `views` + 1"); but $process = mysql_query("INSERT INTO `stats` (`ip`, `views`) VALUES ('$ip_address', 1)
ON DUPLICATE KEY UPDATE `views` = `views` + 1"); you added ;

MrCoder
11-26-2007, 09:01 AM
not $process = mysql_query("INSERT INTO `stats` (`ip`, `views`) VALUES ('$ip_address', 1);
ON DUPLICATE KEY UPDATE `views` = `views` + 1"); but $process = mysql_query("INSERT INTO `stats` (`ip`, `views`) VALUES ('$ip_address', 1)
ON DUPLICATE KEY UPDATE `views` = `views` + 1"); you added ;


<?php
include('other/config.php');

mysql_connect($host, $dbname, $dbpass) or die(mysql_error());
mysql_select_db($dbuser) or die(mysql_error());

$ip_address = $_SERVER['REMOTE_ADDR'];

$process = mysql_query("INSERT INTO `stats` (`ip`, `views`) VALUES ('".mysql_real_escape_string($ip_address)."', 1) ON DUPLICATE KEY UPDATE `views` = `views` + 1") or die(mysql_error());

?>


I think you will find that is the correct solution.