Jump to content

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.

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.