maxso Posted December 23, 2008 Share Posted December 23, 2008 Even though my IP is already in the database and it says 'welcome again', It still keeps adding new rows. Help? <html> <head /> <body> <?php $ip=$_SERVER['REMOTE_ADDR']; $con=mysql_connect('*','*','*'); mysql_select_db('*'); $get=mysql_query("SELECT ip FROM server1 WHERE IP='$ip'"); $ip_exist=mysql_num_rows($get); $add_new=mysql_query("INSERT INTO server1 (IP, count) VALUES ('$ip', '1')"); if($ip_exist > 0){ echo "welcome again"; } else { $add_new; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/138234-solved-else-statement-messed-up/ Share on other sites More sharing options...
maxso Posted December 23, 2008 Author Share Posted December 23, 2008 There should not be a problem as $add_new should only occur when there is not that IP in the table. if($ip_exist > 0){ echo "welcome again"; } else { $add_new; } Quote Link to comment https://forums.phpfreaks.com/topic/138234-solved-else-statement-messed-up/#findComment-722722 Share on other sites More sharing options...
Maq Posted December 23, 2008 Share Posted December 23, 2008 I added some debugging statements that you can take out, but try this: $ip=$_SERVER['REMOTE_ADDR']; $con=mysql_connect('*','*','*'); mysql_select_db('*'); $get=mysql_query("SELECT ip FROM server1 WHERE IP='$ip'"); $ip_exist=mysql_num_rows($get); $sql = "INSERT INTO server1 (IP, count) VALUES ('$ip', '1')"; if($ip_exist > 0){ echo "welcome again"; } else { mysql_query($sql) or die(mysql_error()); echo $sql; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138234-solved-else-statement-messed-up/#findComment-722731 Share on other sites More sharing options...
maxso Posted December 23, 2008 Author Share Posted December 23, 2008 Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/138234-solved-else-statement-messed-up/#findComment-722743 Share on other sites More sharing options...
Maq Posted December 23, 2008 Share Posted December 23, 2008 I assume it works? I think your problem was that you had: $add_new=mysql_query("INSERT INTO server1 (IP, count) VALUES ('$ip', '1')"); Which only held the resource id. If you echo $add_new in the ELSE block you would have gotten something like "Resource Id# 5" or something. I personally always like to put the query in a string so I can debug easily and then just call that string variable in the SQL execution. Oh, please marked as solved if so. Quote Link to comment https://forums.phpfreaks.com/topic/138234-solved-else-statement-messed-up/#findComment-722746 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.