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 關注
- 169 瀏覽
添加回答
舉報