Jump to content

Question about counter


Super2

Recommended Posts

Hi guys,

I`m new to this forum :)

I`m trying to make a php counter, but it just didn`t work...

Here is my code. It returns no error, but it is not working :(


$ip = $_SERVER['REMOTE_ADDR'];
$date = date("d.m.Y");

$query = "SELECT * FROM `visitor`";
$res = mysql_query($query) or die("Greshka ".mysql_error());
while($row = mysql_fetch_array($res)) {

if($date == $row['date']){

if ($ip != $row['ip']){

$query = "INSERT INTO  `visitor` (id, ip , uniqe, date) VALUES ($ip, 1, $date)";
$do = mysql_query($query) or die("Greshka2:".mysql_error()); 

}else{
$impresiq = $row['impresiq'] + 1;
$query ="UPDATE counter SET count = '$count' WHERE ip = '$ip' ";
$do = mysql_query($query) or die("Greshka3:".mysql_error()); 
}

}
}
echo $impresiq." refreshes<br/>\n";
echo "Ip-to vi e ".$ip."<br/> na";
echo $date;

Link to comment
Share on other sites

Is it here?

$impresiq = $row['impresiq'] + 1;
$query ="UPDATE counter SET count = '$count' WHERE ip = '$ip' ";

 

Looks like you're incrementing a different variable than your trying to pass in.

There is no $count in all that code and you're trying to reference it.

Link to comment
Share on other sites

Looks like the insert query is the fault here, you specify 4 coloumns to insert into, but only 3 values to insert. If id is a autonumber coloumn, then remove it from the query.

 

If you have a lot of visitors, the script that you have wrote could take a long time to parse as well, so you could do with changing your method.

Link to comment
Share on other sites

You still have to give it 4 values though right?

 

Just do:

$query = "INSERT INTO  `visitor` (id, ip , uniqe, date) VALUES (NULL, '$ip', '1', '$date')";

 

If it is an auto_increment.

You need to put quotes around the values you pass in too.

Is the word 'uniqe' suppose to be 'unique'?

Link to comment
Share on other sites

You still have to give it 4 values though right?

 

Just do:

$query = "INSERT INTO  `visitor` (id, ip , uniqe, date) VALUES (NULL, '$ip', '1', '$date')";

 

If it is an auto_increment.

You need to put quotes around the values you pass in too.

Is the word 'uniqe' suppose to be 'unique'?

Yeah it is supposed to :D but i already used everywhere that  ;D

 

Now i`m using:

$ip = $_SERVER['REMOTE_ADDR'];
$date = date("d.m.Y");

$query = "SELECT * FROM `visitor`";
$res = mysql_query($query) or die("Greshka ".mysql_error());
while($row = mysql_fetch_array($res)) {

if($date == $row['date']){

if ($ip != $row['ip']){

$query = "INSERT INTO  `visitor` (ip , uniqe, date) VALUES ('$ip', '1', '$date')";
$do = mysql_query($query) or die("Greshka2:".mysql_error()); 

}elseif($row['uniqe'] == 1) {
$impresiq = $row['impresiq'] + 1;
$query ="UPDATE `visitor` SET impresiq = '$impresiq' WHERE ip = '$ip' ";
$do = mysql_query($query) or die("Greshka3:".mysql_error()); 
}

}
}

 

But again there are no signs of live.... The strange is that it returns no error.....

 

p.s. id is auto_increment

ps2. That is my table:

$insert = "CREATE TABLE `visitor` (
`id` int(20) NOT NULL auto_increment,
`ip` varchar(255) default NULL,
`date` varchar(10) default NULL,
`impresiq` varchar(255) default NULL,
`uniqe` varchar(255) default NULL,
PRIMARY KEY (`id`)
)";

Link to comment
Share on other sites

I tried making ip Primary key and using

if ($ip == $row['ip']){

$impresiq = $row['impresiq'] + 1;
$query ="UPDATE `visitor` SET impresiq = '$impresiq' WHERE ip = '$ip' ";
$do = mysql_query($query) or die("Greshka3:".mysql_error()); 

}else{
$query = "INSERT INTO  `visitor` (ip , uniq, data) VALUES ('$ip', '1', '$data')";
$do = mysql_query($query) or die("Greshka2:".mysql_error()); 
}

But it gives me and error....

Duplicate entry '' for key 1
Link to comment
Share on other sites

Duplicate entry '' for key 1

 

That error states that there is already an entry in the table for key 1 that is empty. A variable is not being populated.

 

Also an IP address is not a good idea for a primary key, keep that id in there as IP's are not always unique to users.

Link to comment
Share on other sites

Duplicate entry '' for key 1

 

That error states that there is already an entry in the table for key 1 that is empty. A variable is not being populated.

 

Also an IP address is not a good idea for a primary key, keep that id in there as IP's are not always unique to users.

so ? U suggest to use id as Primary key ? I checked in the DB

ip 	data 	impresiq 	uniq
//*HERE is empty	13.07.2007 	0 	0
	85.187.187.179 	13.07.2007 	0 	1

I have some error in the IF statement i think, but i can`t find it....

I want it like that:

If the ip is new it is recorded in  'ip' and unique is being set to 1.

Or if it is not new > impresiq/refreshes is being increased:

if ($ip == $row['ip']){

$impresiq = $row['impresiq'] + 1;
$query ="UPDATE `visitor` SET impresiq = '$impresiq' WHERE ip = '$ip' ";
$do = mysql_query($query) or die("Greshka3:".mysql_error()); 

}else{
$query = "INSERT INTO  `visitor` (ip , uniq, data) VALUES ('$ip', '1', '$data')";
$do = mysql_query($query) or die("Greshka2:".mysql_error()); 
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.