Jump to content

Trying to limit insertion into db but not working


lovephp

Recommended Posts

Guys i want to limit registration but its not working. say if members table already has 5 rows then i want to show the error registration closed but somehow its now working as in data still getting inserted...

 

//Check number of members
    if($login != '' || $password != '') {
        $qry = "SELECT COUNT(*) FROM  members";
        $result = mysql_query($qry);
        if($result) {
            if(mysql_num_rows($result) > 5) {
                $errmsg_arr[] = 'Cannot add more users';
                $errflag = true;
            }
            @mysql_free_result($result);
        }
        else {
            die("Query failed");
        }
    }

Hi,

 

a COUNT(*) query on the whole table always returns exactly 1 row. Your mysql_num_rows() check makes no sense.

 

Counting the rows before the INSERT query is generally naive and doesn't work when there are multiple requests at the same time. Imagine this scenario: Your table is empty. Then 10 users try to register at the same time, so you count the number of rows. Since it's 0 at that time, you let everybody register. Now you suddenly have 10 users despite your limit of 5.

 

Checks like this are not that simple. Right now, the only solution I could think of would be to put an exclusive lock on the entire table, then count the rows and then insert the row. But this is really, really ugly.

 

Why do you need this weird limit?

lol done. its actually funny everytime i come here asking for help but end up fixing it by myself most of the time ;D

 

did this and i got what i needed

 

//Check number of members
      $query = "SELECT COUNT(*) AS Count FROM members";
      $result = mysql_query($query);
      $result = mysql_fetch_assoc($result);
      $count = $result['Count'];              
      if($count >= 5) {
            $errmsg_arr[] = 'We cannot accept more users right now!';
            $errflag = true;
       }

 

thanks mate appreciate your time

Unfortunately, you seem to have only read the first half of my reply.

 

This does not work. At all. It may seem to work if you have very low traffic and a lot of luck. But this will fall apart at the next opportunity.

 

If that's good enough for you, well, I can't stop you from doing it. But if you care just a tiny bit about correct code, this is simply wrong.

Unfortunately, you seem to have only read the first half of my reply.

 

This does not work. At all. It may seem to work if you have very low traffic and a lot of luck. But this will fall apart at the next opportunity.

 

If that's good enough for you, well, I can't stop you from doing it. But if you care just a tiny bit about correct code, this is simply wrong.

oh mate its just a small script with not too much traffic only for a office use for 15 users not more. yes i read and understood what u wrote thanks alot for the right advise ill keep that in mind. cheers :happy-04:

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.