PHP-如何使 str_shuffle 产生不同的值? $randomcode="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";$randomcode='NT'.substr(str_shuffle($str),5,8); echo $randomcode.'<br>'; echo $randomcode.'<br>';结果:NT2ZCI1qdXNT2ZCI1qdX如何产生不同的值?我想将它插入到我的数据库中。$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]);}非常感谢
1 回答
富国沪深
TA贡献1790条经验 获得超9个赞
您应该在循环内生成代码。
//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) {
//create a "random" string that is 8 characters.
//string will never repeat any characters, and can create duplicates
$code = 'NT' . substr(str_shuffle($charmap), -8);
//send variables to query on each loop
$stmt->execute([':age' => $age, ':randomcode' => $code]);
}
正如我在代码注释中所说,这种方法可能有创建重复代码的倾向。它也不包含任何重复的字母/数字。
- 1 回答
- 0 关注
- 140 浏览
添加回答
举报
0/150
提交
取消