[英]PHP works first time then fails
Basically what this does is makes it so after the user presses a button, they have to wait 2 minutes to press it again. 基本上,这样做是为了使用户按下按钮后,他们必须等待2分钟才能再次按下它。 When you press it the first time, it works, then if you press it again before 2 minutes, it says "please wait 2 minutes." 第一次按下该按钮时,它会起作用,然后如果在2分钟之前再次按下它,它会显示“请等待2分钟”。 but after 2 minutes, you can press it as many times as you like without the error, not what I want. 但是2分钟后,您可以按任意次按下它而不会出现错误,这不是我想要的。 Here is the code 这是代码
<?php
if('sub') {
$client = $_SERVER['REMOTE_ADDR'];
$username="user";
$password="pass";
$database="db";
mysql_connect(localhost,$username,$password);
$time = strtotime("2 minutes");
$now = strtotime("now");
@mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT Time FROM log WHERE IP='$client'";
$result= mysql_query($query);
$num=mysql_num_rows($result);
while($rows=mysql_fetch_array($result)){
$timestamp = $rows['Time'];
}
echo n;
echo $now;
echo t;
echo $timestamp;
if($now < $timestamp)
{
echo "<center><h2 style=\"color:red\" class=\"texts\" id=\"homeLink\">Please wait 2 minutes.</h2></center>";
}
else{
//some other code
$query1 = "INSERT INTO log VALUES ('$client','$time','$username')";
$query2 = "UPDATE `log` SET `Time`='$time' SET `Username`='$username' WHERE `IP`='$client'";
if($num == 0)
{
mysql_query($query1);
}
else
{
mysql_query($query2);
}
mysql_close();
}
?>
As you can see, if there is no row for the IP of the user, it makes a new one, if there is it will update it. 如您所见,如果没有用于用户IP的行,它将创建一个新行,如果存在则将对其进行更新。 After the first time, it no longer works. 第一次之后,它不再起作用。 Hope someone can help. 希望有人能帮忙。 Thanks! 谢谢!
Your update statement is a bit off, so the update fails; 您的更新语句有点偏离,所以更新失败;
UPDATE `log` SET `Time`='$time' SET `Username`='$username' WHERE `IP`='$client'"
You should only use SET
once, like this; 您只能使用一次SET
,就像这样;
UPDATE `log` SET `Time`='$time', `Username`='$username' WHERE `IP`='$client'"
I'm pretty sure your database does not contain entries you think it contains. 我很确定您的数据库不包含您认为包含的条目。 Can you check it (IPs and stuff on after button press)? 您能检查一下吗(按下按钮后IP和其他内容就会显示)?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.