Hi,

I would like to ask a question regarding my code. To give you back ground, I'm making a registration page. The page will auto-generate the user id and it will insert it on the user table. But my problem is that my code is not functioning as expected. It won't generate the ids that I'm expecting. Here is my code to generate the IDs:

for ( $id = 0; $id <= 100; $id++){
	if( isset($_POST['reg'])){
	// print first the id to validate if the code is correct
		echo $id;

}
}

    Hmm... what is your idea behind using a loop?

    Also, you are using a relational database like MySQL, I presume?

      laserlight;10996720 wrote:

      Hmm... what is your idea behind using a loop?

      Also, you are using a relational database like MySQL, I presume?

      I used a loop because I declared 100 users only.

        Sorry, I don't understand what you are trying to do:

        • Are you trying to create 100 users by inserting their details in the database? If so, why do you need a form in the first place? You just need a script that is run once.

        • Are you trying to allow a user to register, and then assign that user an id within the range of 0 to 99?

          laserlight;10996722 wrote:

          Sorry, I don't understand what you are trying to do:

          • Are you trying to create 100 users by inserting their details in the database? If so, why do you need a form in the first place? You just need a script that is run once.

          • Are you trying to allow a user to register, and then assign that user an id within the range of 0 to 99?

          Okay, I'm trying to create a registration page. The user will register on it and the data will be inserted in to the user table and the application will auto-generate their user id. The user table has the user id as its primary key. The user id is the one that I'm having trouble with. I only allow 100 users having 0-99 user IDs, but the code I gave is not working.

            Right. This is the approach I suggest:

            Create the database table with an auto incrementing integer primary key.
            Process the registration form and insert the user details, without specifying the key.
            The database will then insert the row with an appropriate key for the user id.

            Now, the problem is how to limit to 100 users. One approach is to check the number of rows in the database table when processing the registration, just before inserting the row into the database. A more robust approach is to create a constraint on the database table that limits it to 100 rows. You then attempt to insert the row, and then check for a constraint violation so as to report the error to the user.

              laserlight;10996724 wrote:

              Right. This is the approach I suggest:

              Create the database table with an auto incrementing integer primary key.
              Process the registration form and insert the user details, without specifying the key.
              The database will then insert the row with an appropriate key for the user id.

              Now, the problem is how to limit to 100 users. One approach is to check the number of rows in the database table when processing the registration, just before inserting the row into the database. A more robust approach is to create a constraint on the database table that limits it to 100 rows. You then attempt to insert the row, and then check for a constraint violation so as to report the error to the user.

              Thanks for this!

                Write a Reply...