我試圖通過單擊一個按鈕來爲每行更新具有唯一字符串的多行。但是每當我生成它們時,它都會存儲相同的值。和即時通訊有錯誤說「注意:未初始化的字符串偏移:60」。我也希望如果數據庫中已經存在唯一字符串,它必須創建一個新字符串。請幫助。更多的權力!在php mysql中更新多個記錄集中的唯一字符串
這裏是我的代碼:
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);
}
}
是否有任何其他方式來更新我的記錄更新每個獨特的字符串多行? –