简体   繁体   中英

Random number insert mySQL not working

Hey all i am created 2 random numbers like so:

 $firstlink = intval(mt_rand(100, 999) . mt_rand(100, 999) . mt_rand(1, 9) . mt_rand(100, 999)); // 10 digit
 $secondLink = intval(mt_rand(1000, 999) . mt_rand(1, 999) . mt_rand(10, 99) . mt_rand(100, 999));

And this is my insert code:

$result = mysql_query("INSERT INTO userAccount 
                     (Category,Fname,LName,firstlink,secondLink,AccDate) 
                      VALUES (  '" . $cat . "',
                                '" . $fname . "',
                                '" . $lname . "',
                                " . $firstlink . ",
                                " . $secondLink . ",
                                '" . date('Y-m-d g:i:s',time()). "');");

It has no errrs and it places the data into the mysql database. However, its always the same number for BOTH firstlink and secondLink no matter who i add to the database and i have no idea why its doing it!

The datatype for both rows is INT(15)

Remove intval and all will work fine.

$firstlink = mt_rand(100, 999) . mt_rand(100, 999) . mt_rand(1, 9) . mt_rand(100, 999); // 10 digit

32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. With intval you got 2147483647 mostly.

You can simplify your code and improve the randomness of the code like this:

$firstlink = mt_rand(10000,99999) . mt_rand(10000,99999);
$secondLink = mt_rand(10000,99999) . mt_rand(10000,99999);

    echo "INSERT INTO userAccount 
                     (Category,Fname,LName,firstlink,secondLink,AccDate) 
                      VALUES (  '" . $cat . "',
                                '" . $fname . "',
                                '" . $lname . "',
                                " . $firstlink . ",
                                " . $secondLink . ",
                                '" . date('Y-m-d g:i:s',time()). "');"

PHPFiddle: http://phpfiddle.org/main/code/6nf-wpk

This will build your random 10-digit code by making two random 5 digit codes and joining them together. Is is simpler and easier to follow with less parts making it up. It had to be done with two mt_rand()s because the maximum number possible is 2147483647. For each mt_rand() function you're using, you're preventing a 0 from being the first digit in that section of the number, because you're starting the first digit at between 1 and 9.

If you don't care about the first number being only a 1 or 2 (and never being 3-9 or 0) you can simply use

$firstlink = mt_rand(1000000000,2147483647);  // random 10 digit number
$secondLink = mt_rand(1000000000,2147483647); // random 10 digit number

As general coding tips:

  • Be consistent with how you name variables. You have a lowercase "L" in "$firstlink" and a capital "L" in "$secondLink". PHP is case-sensitive and you'll end up using the wrong name and getting unexpected (blank) results elsewhere in your program.
  • Be be careful never to put any user-provided data into a SQL command without protecting against SQL Injection attacks. Use parameterized queries as a rule. See How can I prevent SQL injection in PHP? for more details and examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM