问题
Here is what I wanna do: User can comment up to five times a day (in code I use 60 seconds to see the result quicker). However I seem to make a wrong comparison, because right now it always gives me back true. So even if the counter is above 5, it still counts up instead of stopping there (for 60 seconds). Any input is appreciated. Down below is the code I use.
<?php
$DB_HostName = "";
$DB_Name = "";
$DB_User = "";
$DB_Pass = "";
$DB_Table = "";
$name = mysql_escape_string($_GET['name']);
set_time_limit(0);
$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die (mysql_error());
mysql_select_db($DB_Name,$con) or die(mysql_error());
if ($name[CommentCounterReset] === 5) //or if ("CommentCounterReset === 5 WHERE Name = $name)? {
echo "You already wrote five comments.";
set_time_limit(60);
$sql = "UPDATE table SET CommentCounterReset = CommentCounterReset-5 WHERE Name = '$name'";
} else {
$sql = "UPDATE table SET CommentCounterReset = CommentCounterReset+1 WHERE Name = '$name'";
echo "Comment accepted.";
}
$res = mysql_query($sql,$con) or die(mysql_error());
mysql_close($con);
if ($res) {
}else{
}// end else
?>
回答1:
It looks like you are trying to run the query after a wait time with this command:
set_time_limit(60);
This makes the maximum run time of your script 60 seconds, but doesn't actually do anything more. It changes a setting so your script could run a maximum of 1 minute, but doesn't actually make it run.
You could do it with sleep
but you shouldn't If you want to do it for a day, that means your script would run for a day (it can, sleep doesn't "count" as it is a system call).
Better write a certain time to your database and check against that. If the user wants to comment you check
Check if the user has 5 comments.
- Yes? check the time
1.if it is in the past, post the comment and reset the counter.- if it is in the future, don't post the comment.
- No? increase the counter and post the comment
- did you increase it to 5? then set the date to +60 seconds (or +1 day).
回答2:
make correction here,it must greater than equal to
if ($name[CommentCounterReset] >= 5)
来源:https://stackoverflow.com/questions/12544658/check-for-specific-integer-in-a-row-where-user-name