Super2 Posted July 12, 2007 Share Posted July 12, 2007 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 https://forums.phpfreaks.com/topic/59646-question-about-counter/ Share on other sites More sharing options...
soycharliente Posted July 12, 2007 Share Posted July 12, 2007 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 https://forums.phpfreaks.com/topic/59646-question-about-counter/#findComment-296409 Share on other sites More sharing options...
metrostars Posted July 12, 2007 Share Posted July 12, 2007 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 https://forums.phpfreaks.com/topic/59646-question-about-counter/#findComment-296413 Share on other sites More sharing options...
soycharliente Posted July 12, 2007 Share Posted July 12, 2007 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 https://forums.phpfreaks.com/topic/59646-question-about-counter/#findComment-296415 Share on other sites More sharing options...
Super2 Posted July 12, 2007 Author Share Posted July 12, 2007 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 but i already used everywhere that 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 https://forums.phpfreaks.com/topic/59646-question-about-counter/#findComment-296619 Share on other sites More sharing options...
Super2 Posted July 12, 2007 Author Share Posted July 12, 2007 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 https://forums.phpfreaks.com/topic/59646-question-about-counter/#findComment-296750 Share on other sites More sharing options...
per1os Posted July 12, 2007 Share Posted July 12, 2007 $ip does not contain a value. Link to comment https://forums.phpfreaks.com/topic/59646-question-about-counter/#findComment-296761 Share on other sites More sharing options...
Super2 Posted July 12, 2007 Author Share Posted July 12, 2007 $ip does not contain a value. It has $ip = $_SERVER['REMOTE_ADDR']; And it`s adding the data only ones... Then it seems to be trying to add it one more time instead of adding $impresiq .... Link to comment https://forums.phpfreaks.com/topic/59646-question-about-counter/#findComment-296778 Share on other sites More sharing options...
per1os Posted July 12, 2007 Share Posted July 12, 2007 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 https://forums.phpfreaks.com/topic/59646-question-about-counter/#findComment-296781 Share on other sites More sharing options...
Super2 Posted July 13, 2007 Author Share Posted July 13, 2007 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 https://forums.phpfreaks.com/topic/59646-question-about-counter/#findComment-297272 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.