Jump to content

Easy PHP / MySQL question (just not easy for me)


jessl1905

Recommended Posts

I'm not a coder unfortunately, however, I am trying to work out the best way (and to learn how to) have data inputted into the mysql database at random.

 

ie: There is 100 rows in the Database (pre-created to have values in a second column). I enter a name using a form and it inserts the name into a random row in the first column, only if that row is empty. If the row is not empty and it hasn't gone through all the 100 rows, continue checking and then insert once there's an empty row. If it has checked all 100 rows and they're all full, alert or echo with "Database is full!".

 

Or would it be better for me to do this on the PHP side, where the randomness is done there and then inserted into the database?

Any help with this would be much appreciated!

 

Ta

Jess

 

Link to comment
Share on other sites

Your logic is a little crazy here.

 

Why not just keep adding users to the database until you've reached 100? Keep the values you'd have in the second column in a separate table, and combine them when you need to make pairs.

 

Once you've hit 100 entries in the database, just return the 'DB is full' message until that number drops.

Link to comment
Share on other sites

Thank you xyph!

Makes perfect sense.

 

So if I create the table 'users' (which is empty) and create another table 'number' (100 rows with values), how could I use PHP to go:

 

on submit, insert a new user (name as the input) into 'users', get a random value from 'number' and assign it to the user, mark that value from 'number' as "used up so no other name can get that value", and stop running all this once the 100 users have been added.

 

It's quite confusing for me to understand how to do this in PHP..

 

Thank you again for your help, I really appreciate it!!

Link to comment
Share on other sites

Don't worry about giving a user a random number until you want to use that information.

 

If you told me, in a broader sense, what you're trying to accomplish, I could help further.

 

Ideally, when you want to give the users a random number, you'll grab all the users into an array, all of the numbers into an array, shuffle one of them, and combine.

 

I'll start providing some coded examples once you start showing us some coded attempts.

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.