Jump to content

[SOLVED] Else statement messed up


maxso

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/138234-solved-else-statement-messed-up/
Share on other sites

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;
}

?>


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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.