Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php/mysql中生成随机字符串_Php_Mysql_Sql - Fatal编程技术网

在php/mysql中生成随机字符串

在php/mysql中生成随机字符串,php,mysql,sql,Php,Mysql,Sql,我需要在mysql中插入数据时生成一个随机字符串。 我确实读过关于uuid或cast(rand)的文章,但我找不到任何看起来我可以使用它的东西 我的数据来自一个应用程序 我做了一个新的行,名为code,并使其成为unik 我希望你能帮助我:) 如何让insert向行代码生成随机字符串 // array for JSON response $response = array(); // check for required fields if (isset($_POST['name']) &a

我需要在mysql中插入数据时生成一个随机字符串。 我确实读过关于uuid或cast(rand)的文章,但我找不到任何看起来我可以使用它的东西

我的数据来自一个应用程序

我做了一个新的行,名为code,并使其成为unik

我希望你能帮助我:)

如何让insert向行代码生成随机字符串

// array for JSON response
$response = array();

 // check for required fields
if (isset($_POST['name']) && isset($_POST['nummer']) && isset($_POST['description']) && isset($_POST['dato'])) {

$name = $_POST['name'];
$nummer = $_POST['nummer'];
$description = $_POST['description'];
$dato = $_POST['dato'];

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

mysql_set_charset("utf8");

// mysql inserting a new row
$result = mysql_query("INSERT INTO products(name, nummer, description, dato) VALUES('$name', '$nummer', '$description', '$dato')");
好的,这就是我目前得到的

if (isset($_POST['name']) && isset($_POST['nummer']) && isset($_POST['description']) && isset($_POST['dato'])) {

$name = $_POST['name'];
$nummer = $_POST['nummer'];
$description = $_POST['description'];
$dato = $_POST['dato'];
// $code = $_POST['code'];

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

  function generate_random_string($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
  }


// mysql inserting a new row
$result = mysql_query("INSERT INTO products(name, nummer, description, dato, code) VALUES('$name', '$nummer', '$description', '$dato', '$randomString')");

您可以尝试以下代码:

    function generate_random_string($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;
    }

echo generate_random_string(15);  // define amount of string length in parametre

您可以使用PHP函数
rand
生成一个随机数:

// code = rand($min , $max); for example
code = rand(100000, 999999); // will generate a number between 100000 and 999999
// then add the column to your insert if it belongs to the same table or 
// make another query to insert the code if it belongs to a different table
请注意,您可以使用当前时间和
md5
功能生成更强的唯一代码

此外,如果希望将此逻辑带到数据库中,请尝试创建一个触发器,该触发器将在每次插入后运行


祝你好运

如果要在PHP中生成它:。它们不再得到维护。看到了吗?改为了解,并在复制/删除某人的代码时使用或给予信任或链接。您没有正确使用。首先在变量中赋值函数的值。like$random\u str=generate\u random\u string(10);然后使用queryreturn$randomString中的变量$random_str=generate_random_string(10);}//mysql插入新行$result=mysql_查询(“插入到产品(名称、数字、描述、数据、代码)值(“$name”、“$numer”、“$description”、“$dato”、“$random_str”));还是没什么。不要改变功能,在外面做,不要在功能中做。我在手机里,当我在电脑里的时候,我会清除你的“MD5”和“更强”不属于同一个句子。@tadman在当前时间使用str_shuffle连接生成的随机数,然后在那个字符串上使用MD5对我来说就足够了,我知道作为密码使用它并不安全,但是对于确认链接,它做得很好。如果你使用MD5,你可能做错了。要么你想要一些真正安全的东西,在这种情况下它是完全不合适的,要么你不想要,在这种情况下,默认的随机数生成器,正确的种子,是可以的。我完全同意,当然MD5是不安全的,我不使用它的目的是创建它,但它给了我好看的32字符字符串,仅此而已。但是,是的,对于激活,例如,随机4位数字就足够了。
$number = range(100000,999999);
shuffle($number);
$ran_string = $number[0];
echo $ran_string;
// code = rand($min , $max); for example
code = rand(100000, 999999); // will generate a number between 100000 and 999999
// then add the column to your insert if it belongs to the same table or 
// make another query to insert the code if it belongs to a different table