Jump to content

Archived

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

birdie

adding from timestamps in mysql

Recommended Posts

[code]

$ip = $_SERVER['REMOTE_ADDR'];
$sql = "SELECT * FROM portal_logintrys WHERE ip='$ip'";
$query = mysql_query($sql);
$numrows = mysql_num_rows($query);
$object = mysql_fetch_object($query);

if($numrows == "0")
{
$sql = "INSERT INTO portal_logintrys ('ip','times') VALUES ('$ip','1')";
}
else
{

$times2 = $object->times;
$times2++;
$sql = "UPDATE portal_logintrys SET times='$times2' WHERE ip='$ip'";
}
mysql_query($sql);

[/code]

completely confused, please tell me why this script wont even touch mysql..

Share this post


Link to post
Share on other sites
You don't seem to be connecting to the database, at least not in the code you've shown, so that would be a necessary first step. What error do you get when you run that code?

Share this post


Link to post
Share on other sites
[!--quoteo(post=382677:date=Jun 12 2006, 12:05 AM:name=Buyocat)--][div class=\'quotetop\']QUOTE(Buyocat @ Jun 12 2006, 12:05 AM) [snapback]382677[/snapback][/div][div class=\'quotemain\'][!--quotec--]
You don't seem to be connecting to the database, at least not in the code you've shown, so that would be a necessary first step. What error do you get when you run that code?
[/quote]

lol trust me, i'm connected. No errors, i'm guessing the vars are messed up but i just cant see it

Share this post


Link to post
Share on other sites
Try know ok.


[code]

$ip = $_SERVER['REMOTE_ADDR'];

$sql = "SELECT * FROM portal_logintrys WHERE ip='$ip'";
$query = mysql_query($sql);

$numrows = mysql_num_rows($query);

$object = mysql_fetch_object($query);

if($numrows == "0") {

$sql_insert= "INSERT INTO portal_logintrys ('ip','times') VALUES ('$ip','1')";
mysql_query($sql_insert);

}else{

$times2 = $object->times;

$times2++;

$sql_update = "UPDATE portal_logintrys SET times='$times2' WHERE ip='$ip'";
mysql_query($sql_update);
}

[/code]

Share this post


Link to post
Share on other sites
Just a FYI, you don't need to use the $times variable. You can UPDATE the count directly with MySQL, like:

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']UPDATE[/span] portal_logintrys SET times[color=orange]=[/color]times[color=orange]+[/color]1 [color=green]WHERE[/color] ip[color=orange]=[/color][color=red]'$ip'[/color] [!--sql2--][/div][!--sql3--]

Also I'll suppose you are going to use this to avoid people from trying to crack passwords, but this is useless if you don't set a reset time for the attempts per IP. For this to work, you should add a field containing the timestamp of the first try.

Share this post


Link to post
Share on other sites

×

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.