我试图通过单击按钮为每行更新具有唯一字符串的多行。但是每当我生成它们时,它都会存储相同的值。我有一个错误说“注意:未初始化的字符串偏移量:60”。我还希望如果数据库中已经存在唯一字符串,它必须创建一个新字符串。请帮忙。更多的力量!
这是我的代码:
if(isset($_POST['gen'])){
function genRandomString() {
$length = 6;
$characters = '023456789abcdefghijkmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM';
$string = '';
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
return $string;
}
$q = mysql_query("SELECT * FROM exam_passwords WHERE pass_subject_id = ".$subject_id."")or die(mysql_error());
$r = mysql_num_rows($q);
for($i = 0; $i<$r; $i++){
$random_string = genRandomString();
$k = "UPDATE exam_passwords SET pass_password = '$random_string' WHERE pass_subject_id = '$subject_id'";
$result = mysql_query($k);
}
}