简体   繁体   English

随机数插入MySQL无法正常工作

[英]Random number insert mySQL not working

Hey all i am created 2 random numbers like so: 嘿,我创建了2个像这样的随机数:

 $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. 它没有错误,并且将数据放入mysql数据库。 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! 然而,它始终为BOTH 首联是secondlink相同数量不管我谁添加到数据库中,我不知道为什么它这样做!

The datatype for both rows is INT(15) 两行的数据类型均为INT(15)

Remove intval and all will work fine. 删除intval,一切正常。

$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. 32位系统的最大有符号整数范围为-2147483648至2147483647。使用intval时,您最多可获得2147483647。

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 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. 通过制作两个随机的5位数代码并将它们连接在一起,可以构建10位数的随机代码。 Is is simpler and easier to follow with less parts making it up. Is变得更简单,并且只需更少的零件即可轻松完成。 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. 必须使用两个mt_rand()来完成,因为可能的最大数量为2147483647。对于您使用的每个mt_rand()函数,要防止0成为该部分中的第一位数字,因为从1到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 如果您不关心第一个数字仅为1或2(从不为3-9或0),则可以简单地使用

$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". 在“ $ firstlink”中有一个小写的“ L”,在“ $ secondLink”中有一个大写的“ L”。 PHP is case-sensitive and you'll end up using the wrong name and getting unexpected (blank) results elsewhere in your program. PHP区分大小写,您将最终使用错误的名称,并在程序的其他位置得到意外的(空白)结果。
  • Be be careful never to put any user-provided data into a SQL command without protecting against SQL Injection attacks. 注意不要在没有防止SQL注入攻击的情况下将任何用户提供的数据放入SQL命令。 Use parameterized queries as a rule. 通常使用参数化查询。 See How can I prevent SQL injection in PHP? 请参阅如何防止在PHP中进行SQL注入? for more details and examples. 有关更多详细信息和示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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