/    Sign up×
Community /Pin to ProfileBookmark

insert into database?

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.

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 24.2007 — 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:
[code=php]
// 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";
[/code]
Copy linkTweet thisAlerts:
@evaniaauthorNov 24.2007 — 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:
[code=php]
// 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";
[/code]
[/QUOTE]


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?
Copy linkTweet thisAlerts:
@h3r0Nov 25.2007 — Use this code to get the user's IP address:

[code=php]$ip_address = $_SERVER['REMOTE_ADDR'];[/code]
Copy linkTweet thisAlerts:
@evaniaauthorNov 25.2007 — Use this code to get the user's IP address:

[code=php]$ip_address = $_SERVER['REMOTE_ADDR'];[/code][/QUOTE]



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

[code=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'];

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

ecco mysql_error()

?>[/code]
Copy linkTweet thisAlerts:
@h3r0Nov 25.2007 — Yeah looks like it. Test it out see if it works. Learn by experience.
Copy linkTweet thisAlerts:
@evaniaauthorNov 26.2007 — Yeah looks like it. Test it out see if it works. Learn by experience.[/QUOTE]

i get no error when i run it but also no entry is added to the database! ?
Copy linkTweet thisAlerts:
@mitchellNov 26.2007 — try this:

[code=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 ('$ip_address', 1);
ON DUPLICATE KEY UPDATE views = views + 1");
or die(mysql_error());

?>[/code]
Copy linkTweet thisAlerts:
@evaniaauthorNov 26.2007 — try this:

[code=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 ('$ip_address', 1);
ON DUPLICATE KEY UPDATE views = views + 1");
or die(mysql_error());

?>[/code]
[/QUOTE]


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

:/
Copy linkTweet thisAlerts:
@roscorNov 26.2007 — not [code=php]$process = mysql_query("INSERT INTO stats (ip, views) VALUES ('$ip_address', 1);
ON DUPLICATE KEY UPDATE views = views + 1"); [/code]
but [code=php]$process = mysql_query("INSERT INTO stats (ip, views) VALUES ('$ip_address', 1)
ON DUPLICATE KEY UPDATE views = views + 1"); [/code]
you added [B];[/B]
Copy linkTweet thisAlerts:
@MrCoderNov 26.2007 — not [code=php]$process = mysql_query("INSERT INTO stats (ip, views) VALUES ('$ip_address', 1);
ON DUPLICATE KEY UPDATE views = views + 1"); [/code]
but [code=php]$process = mysql_query("INSERT INTO stats (ip, views) VALUES ('$ip_address', 1)
ON DUPLICATE KEY UPDATE views = views + 1"); [/code]
you added [B];[/B][/QUOTE]


[code=php]
<?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());

?>
[/code]


I think you will find that is the correct solution.
×

Success!

Help @evania spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...