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:

[PHP]if(isset($_REQUEST['Generate']))
{
$nopwd= $_POST['nopwd'];//no of password
$length = $_POST['length'];//lenght of password
$value1= $_POST['pwdchar1'];//abcdefghijklmnopqrstuvwxyz
$value2= $_POST['pwdchar2'];//ABCDEFGHIJKLMNOPQRSTUVWXYZ
$value3= $_POST['pwdchar3'];//0123456789
$chars = $value1."".$value2."".$value3;

$max_i = strlen($chars)-1;

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

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


ronverdonk
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++) {
$value="";
for($j=0; $j<$length; $j++) {
$value .= $chars{mt_rand(0,$max_i)};
}
$newpwds[] = $value; //insert new index into array
}[/php]
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.

[PHP]for($x=0;$x<sizeof($newpwds);$x++)
{
echo "Password".(1+$x)." :\t".$newpwds[$x]."<br>";
}[/PHP]
Regard,
Fareast Adam
Mar 14 '07 #3

Post your reply

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