PHP-如何使 str_shuffle 产生不同的值?

PHP-如何使 str_shuffle 产生不同的值?


    $randomcode="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";

$randomcode='NT'.substr(str_shuffle($str),5,8);

    echo $randomcode.'<br>';

    echo $randomcode.'<br>';

结果:


NT2ZCI1qdX

NT2ZCI1qdX

如何产生不同的值?我想将它插入到我的数据库中。


$randomcode="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";

$randomcode=substr(str_shuffle($str),5,8);


$stmt = $pdo->prepare("insert into AGEGroup (AGE,hidecode) values (:age,$randomcode)");


foreach ($_POST['age'] as $age) {

    $stmt->execute([':age'=>$age]);

}

非常感谢


MM们
浏览 141回答 1
1回答

富国沪深

您应该在循环内生成代码。//list of possible characters in generated code$charmap = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";//note using `:randomcode` instead of the direct variable$stmt = $pdo->prepare("insert into AGEGroup (AGE) values (:age, :randomcode)");foreach ($_POST['age'] as $age) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; //create a "random" string that is 8 characters.&nbsp; &nbsp; //string will never repeat any characters, and can create duplicates&nbsp; &nbsp; $code = 'NT' . substr(str_shuffle($charmap), -8);&nbsp; &nbsp; //send variables to query on each loop&nbsp; &nbsp; $stmt->execute([':age' => $age, ':randomcode' => $code]);}正如我在代码注释中所说,这种方法可能有创建重复代码的倾向。它也不包含任何重复的字母/数字。
打开App,查看更多内容
随时随地看视频慕课网APP