Hi, I have this code while($rndmpnts = mysql_fetch_array($random_points_sql)) { $calculate = $rndmpnts['time_last'] + $time_now; if ($calculate > $time_now) { echo "<div class='alert-box error'><span>Error:</span> </div>"; } else { mysql_query("UPDATE 'users' SET points = '$new_points' WHERE username = '$my_username', user_id = '$my_id'"); mysql_query("UPDATE 'random_points' SET 'time_last` = '$time_now' WHERE 'username'' = '$my_username', 'user_id' = '$my_id'"); } } else { mysql_query("UPDATE 'users' SET points = '$new_points' WHERE 'username' = '$my_username', 'user_id' = '$my_id'"); mysql_query("INSERT into 'random_points'('username', 'user_id', 'time_last', 'points_last') VALUES ('$my_username', '$my_id', '$time_now', '$rand_points')"); } PHP: So if $calculate > $time_now I get error else I've update database. But I need 3rd else here. How can be done? Can be done 3rd else to become if? while($rndmpnts = mysql_fetch_array($random_points_sql)) { $calculate = $rndmpnts['time_last'] + $time_now; if ($calculate > $time_now) { echo "<div class='alert-box error'><span>Error:</span> </div>"; } else { mysql_query("UPDATE 'users' SET points = '$new_points' WHERE username = '$my_username', user_id = '$my_id'"); mysql_query("UPDATE 'random_points' SET 'time_last` = '$time_now' WHERE 'username'' = '$my_username', 'user_id' = '$my_id'"); } } if ($calculate < $time_now){ mysql_query("UPDATE 'users' SET points = '$new_points' WHERE 'username' = '$my_username', 'user_id' = '$my_id'"); mysql_query("INSERT into 'random_points'('username', 'user_id', 'time_last', 'points_last') VALUES ('$my_username', '$my_id', '$time_now', '$rand_points')"); } PHP:
That will always trigger, unless the $calculate and $time_now variables are exactly the same. (basically, what you're doing is checking for one condition, and if that isn't true, you trigger the first else-block. I'm assuming that what you're trying to do is check the variable, and if it's higher than $time_now, throw an error (which isn't defined), and if not, update the rows of they exist, and if they don't exist, insert a new row?
Then you need to check and see if the query returns any rows, and if not, insert. If it does, update. Besides, you shouldn't be using mysql_ - it's deprecated, and full of potential security holes. Use mysqli_ or PDO instead, and prepared queries.
My problem is that my code is like first snippet - 1 if with two else`s and I get error "Parse error: syntax error, unexpected 'else' (T_ELSE) in..." .. Now I know what it means but I don't know how exactly to construct this IF-ElSEIF-ELSE . In code like this what can I put for second condition in elseif() ? $calculate<$time_now?
You're putting the second else as an else to the while-loop, which won't work. As I said, you need to check the result returned from the query to see if there should be inserted or updated. Also, elseif needs to be done before the last else (if, elseif, else) and it needs to be within the loop. I'm on my phone, which makes writing code a hassle, else I would write an example.
Try to use Switch Case for conditions like these. Its more optimized and you can have a default section which runs in case values doesn't match anything. Also you can run multiple sections by not putting a break statement in code after section finishes. Check php doc for switch case for more information on this.
We need to do small correction for your code, What you are doing is: If calculate time is greater than time now, then you are showing error messages, and in remaining cases you are updating the query. But you need to check one more condition then only you need to update.We can resolved it with IF-ElSEIF-ELSE like below. while($rndmpnts = mysql_fetch_array($random_points_sql)) { $calculate = $rndmpnts['time_last'] + $time_now; if ($calculate > $time_now) { // your error code echo "<div class='alert-box error'><span>Error:</span> </div>"; } else if ($calculate < $time_now){ // if above condition is failed then this block will be executed, write your code here } else { //If above 2 conditioned failed then this block will be executed mysql_query("UPDATE 'users' SET points = '$new_points' WHERE username = '$my_username', user_id = '$my_id'"); mysql_query("UPDATE 'random_points' SET 'time_last` = '$time_now' WHERE 'username'' = '$my_username', 'user_id' = '$my_id'"); } }
What if I get the second else out of the while loop? Like this while($rndmpnts = mysql_fetch_array($random_points_sql)) { $calculate = $rndmpnts['time_last'] + $time_now; if ($calculate > $time_now) { echo "<div class='alert-box error'><span>Error:</span></div>"; } else { mysql_query("UPDATE `users` SET points = '$new_points' WHERE username = '$my_username', user_id = '$my_id'"); mysql_query("UPDATE `random_points` SET `time_last` = '$time_now' WHERE `username` = '$my_username', `user_id` = '$my_id'"); } } else { mysql_query("UPDATE `users` SET points = '$new_points' WHERE `username` = '$my_username', `user_id` = '$my_id'"); mysql_query("INSERT into `random_points`(`username`, `user_id`, `time_last`, `points_last`) VALUES ('$my_username', '$my_id', '$time_now', '$rand_points')"); } } PHP: In this case I think I don't need second condition or I'm wrong?
Actually this process will not give result what you are expecting. If you write update condition out of the while loop then it will check with last record only. But it will not check remaining records, that is why we need to keep update query in while loop only.
Actually, an "else" after a while-loop will fail - it will throw an error, simply because you're lacking the if {}-statement for the else {} - so it won't check the "last record", it will fail, with either a critical error or a warning (I'm not sure which, I couldn't be bothered testing it right now.