I would personally simplify it, for example separating the queries in a logical manner and check if the account exists before checking if they have a higher count, I had commented the code to better give you an understanding of what I have done.
<?php
if( !isset($_SESSION) ) { session_start(); }
$database_db="cat";
$user_db="root";
$password_db="cyberworld";
$host_db="localhost";
// connect to database
mysql_connect($host_db,$user_db,$password_db) or die ("couldnot connect: ".mysql_error());
mysql_select_db($database_db, $link) or exit('Error Selecting database: '.mysql_error()); ;
// escape user input for database protection
$userid = mysql_real_escape_string($_POST["userid"]);
$password = mysql_real_escape_string($_POST["password"]);
// get user count from database, as that is all we need
$user = "SELECT count FROM usertab WHERE userid='$userid' AND password='$password'";
$userres = mysql_query($user);
// fetch the single user row we had selected
$row = mysql_fetch_row($userres);
// check if the user exists first
if(mysql_num_rows($userres) != 0) {
// check if the user has logged in 3 times already
if($row['count'] > 3) {
echo "Error - You have logged in three times.";
} else {
//successful login, increment count by one
mysql_query("UPDATE usertab SET count = count + 1 WHERE userid = '$userid'");
//redirect them to the page
header("Location: insert1.php");
}
} else {
//if their username and password returned nothing
echo "Error - Your account does not seem to exist.";
}
?>Note I have not tested this, if there is an error it is most likely something simple, you can ask.
Edited by Alexander, 17 February 2011 - 09:56 PM.
Be sure to read the updated
FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us
why or
what errors occurred.