Jump to content

Trying to limit insertion into db but not working


lovephp
Go to solution Solved by 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");
        }
    }

Edited by lovephp
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • Solution

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.