By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,320 Members | 2,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,320 IT Pros & Developers. It's quick & easy.

Insert multiple random password into mysql

P: 51
Here is my code:

$nopwd= $_POST['nopwd'];//no of password
$length = $_POST['length'];//lenght of password
$value1= $_POST['pwdchar1'];//abcdefghijklmnopqrstuvwxyz
$value3= $_POST['pwdchar3'];//0123456789
$chars = $value1."".$value2."".$value3;

$max_i = strlen($chars)-1;

$newpwds = array();//array for random password
$value[$i] .= $chars{mt_rand(0,$max_i)};
$newpwds[$i] = $value[$i];//insert new index into array
//insert into db all array index
mysql_query("UPDATE user SET password='".$newpwds[$x]."' WHERE accessLevel1='2' AND deptID='20'") or die(mysql_error());

Why all row ($nopwd) receives same random password (a value)?
Mar 13 '07 #1
Share this Question
Share on Google+
2 Replies

Expert 2.5K+
P: 4,258
You have several errors in your code. Main is that you are (a) initializing the array each time (b) replace the same chars in the array, so your passwords are always identical. The first 'for' loop must be replaced with this code.
[php] $newpwds = array(); //array for random password
for($i=0; $i<$nopwd; $i++) {
for($j=0; $j<$length; $j++) {
$value .= $chars{mt_rand(0,$max_i)};
$newpwds[] = $value; //insert new index into array
Ronald :cool:
Mar 13 '07 #2

P: 51
Thanks Ronald for ur help but i still got same output. I try to split one by one code all went ok. I expect the mysql update code not running carefully coz when i just display an array index (without include update code) it running ok.

echo "Password".(1+$x)." :\t".$newpwds[$x]."<br>";
Fareast Adam
Mar 14 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.