Insert multiple random password into mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fareast Adam
    New Member
    • Feb 2007
    • 51

    Insert multiple random password into mysql

    Here is my code:

    [PHP]if(isset($_REQU EST['Generate']))
    {
    $nopwd= $_POST['nopwd'];//no of password
    $length = $_POST['length'];//lenght of password
    $value1= $_POST['pwdchar1'];//abcdefghijklmno pqrstuvwxyz
    $value2= $_POST['pwdchar2'];//ABCDEFGHIJKLMNO PQRSTUVWXYZ
    $value3= $_POST['pwdchar3'];//0123456789
    $chars = $value1."".$val ue2."".$value3;

    $max_i = strlen($chars)-1;

    for($i=0;$i<$no pwd;$i++)
    {
    $newpwds = array();//array for random password
    for($j=0;$j<$le ngth;$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<siz eof($newpwds);$ x++)
    {
    mysql_query("UP DATE user SET password='".$ne wpwds[$x]."' WHERE accessLevel1='2 ' AND deptID='20'") or die(mysql_error ());
    }
    }[/PHP]

    Why all row ($nopwd) receives same random password (a value)?
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    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:

    Comment

    • Fareast Adam
      New Member
      • Feb 2007
      • 51

      #3
      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<siz eof($newpwds);$ x++)
      {
      echo "Password".(1+$ x)." :\t".$newpwds[$x]."<br>";
      }[/PHP]
      Regard,
      Fareast Adam

      Comment

      Working...