0

I cant seem to work out why my random number is generating the same result on refresh or back btn on the browser. or if there is an error with the email such as a duplicate entry the header saids to go back to the register page to fill in details again.

Briefly: I'm trying to create a new number sequence in my database every time I register a new user, what's happening is the users are receiving the same random number most times if i was to refresh the form or press back on the browser, the number sequence is called only on submit

<?php

$create_account = substr(number_format(time() * rand(),0,'',''),0,10);

 //etc

if ($insert_stmt = $mysqli->prepare("INSERT INTO members (username, email,
     password,salt, account, registered ) VALUES (?, ?, ?, ?,$create_account, now())")) 

 //  Database looks like this after it is inserted, check the account column.


...................................................................................
| id  | username |      email      |  paswword  |  salt   |  account  | registered |
....................................................................................
|  1  | david    | [email protected]    |dfg8dfgdfgg |dfgdfgdfg| 2323232329 | 10/8/2013  |
....................................................................................
|   2 | bill     |  [email protected]   |sdfsdfsdfspz|wpzz9kecm| 2323232329 | 11/26/2013 |
....................................................................................

I dont understand why this function is not recalculating every-time the page.php is requested once i fill the form for a new user.

check the image:example of storing old variables

1 Answer 1

2

Seems your account column type's max integer is 232323231, while your function outputs a 10 digit number. Fix your table to bigint(20) unsigned and it should work. :)

Sign up to request clarification or add additional context in comments.

8 Comments

The value you has isn't one of the max values based on data type. You should seed rand() before you try to generate a random number with it. - Okay, the edited question ACTUALLY IS a max value. Nevermind.
Not in PHP. You're confusing it with JavaScript's Math.random(). EDIT: damn it, comment I commented this to got deleted :P
I upadated with image to help see the issue, if i register the user and then its successful, then i press on the back btn of the browser and register another user this random number is the same.
Yea, seems it's even maxing out the max 32bit signed integer. Either go int(10) unsigned, bigint(20) unsigned or varchar(20) and it would be wise to give rand() a min and max value.
i went with varchar 20, it works good enough for me and thank you for taking the time to help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.