Jump to content

Counting the number of occurences in database


AdRock

Recommended Posts

I am having problems with this if statement takign the wrong branch

 

If the user wants to add a record, i query the database to see if they have less than 3 records in there.  If they have, perform the insert, if they have 3, display the error message to them.

 

I have one record in the database but i am getting shown the error

 

I want to count the number of times the userid occurs in the database and that is what i am using to compare against but it is not working

$result = mysql_query("SELECT COUNT(userid) FROM carshare WHERE userid='".$_SESSION['user_id']."'") or die (mysql_error());

$numrows = mysql_fetch_row($result);

if($numrows < 3) {
    //query to enter form data into database
    $sql = mysql_query("INSERT INTO carshare (userid, seats_available, start_street, start_postcode, start_lat, start_long, end_street, end_postcode, end_lat, end_long, depart_time) 
    	VALUES('$userid', '$seats', '$s_street', '$s_postcode', '$s_lat', '$s_long', '$e_street', '$e_postcode', '$e_lat', '$e_long', '$depart'");    	    

    //if the query didn't complete display error message
    if(!$sql) { 
    	echo "There has been an error adding your carshare. Please contact the webmaster via contact page."; 
    	    }
}
else {
    echo "<h2 class=\"errorhead\">Warning - You are only allowed to add 3 car shares!!</h2>";
}

 

Archived

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

×
×
  • Create New...

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.