1

i just want to have random password from all the query results i did of the students. on my situation, i tried to generate but it returns only 1 generated password to all queried students(about 45 students). how should i make a random one. plss help.. heres my code

   <?php
   if(isset($_POST['generate'])){
   $charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()_+';
    $generated_pass = substr(str_shuffle($charset), 0, 12);
    $generate = $_POST['generate'];
    }
    ?>
    <form method="post" action='enroll_student.php' name ='register'>

     <?php
     $yr = date("Y");
     if ($result = $mysqli->query("SELECT
     tbl_studentreg.studId,
     tbl_studentreg.fname,
     tbl_studentreg.lname,
     tbl_studentreg.mname,
     tbl_studentreg.dob,
     tbl_studentreg.address,
     tbl_department.departmentName,
     tbl_studentreg.sy
     FROM tbl_studentreg
     Inner Join tbl_department ON tbl_studentreg.departmentId = tbl_department.departmentId WHERE tbl_studentreg.sy =  '$yr' "))
                    {
       if ($result->num_rows > 0)
       {
                echo "<table width= '1000'>";
                echo "<tr><th>StudentID</th><th>Name</th><th>Date of Birth</th><th>Address</th><th>Department</th><th>School Year</th><th>Password</th></tr>";

         while ($row = $result->fetch_object())
          {
           echo "<tr>";

        echo "<td align='center'>" . $row->studId . "</td>";  
        echo "<td align='center'>" . $row->fname . " ". $row->mname ." ". $row->lname ." </td>";
    echo "<td align='center'>".$row->dob."</td>";
    echo "<td align='center'>" . $row->address. "</td>";
    echo "<td align='center'>".$row->departmentName."</td>";
    echo "<td align='center'>".$row->sy."</td>";

   if(isset($generated_pass)) {
        //how could i make this one generate random password for every students..?
    for($i=0; $i <= $row->studId; $i++){              
    echo "<td>$generated_pass</td>";
        }
        }
  echo "</tr>";
        }
echo "</table>";
                }
              else
                {
              echo "No Results.";
                }
                }
              else
                {
              echo "Error: " . $mysqli->error;
                 }
            $mysqli->close();
            echo '<br>';
    include 'count.php'; //this one will give the total no. of results, just ignore.
            ?>

               <br />        

          <tr><td></td></tr><tr><td><input type='submit' name='generate' value='Generate'/></td></tr>
        </table>
          </form>
Pot Pot
  • 27
  • 2
  • 9
  • 1
    refer [this](http://stackoverflow.com/questions/1837432/how-to-generate-random-password-with-php?rq=1) and [this](http://stackoverflow.com/questions/6101956/generating-a-random-password-in-php?rq=1) too. – Bhavik Shah Feb 19 '13 at 06:22
  • You need to make the password generator a function and call it for each new password you need. You are only creating it one time and using the same one over and over. – Danny Feb 19 '13 at 06:23
  • your password is generated just once – fefe Feb 19 '13 at 06:26
  • @Danny...ah ok... should i make another query for the function again?.. – Pot Pot Feb 19 '13 at 06:35
  • possible duplicate of [PHP random string generator](http://stackoverflow.com/questions/4356289/php-random-string-generator) – Gajus Apr 13 '14 at 16:38

1 Answers1

1
function genpass(){
$charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()_+';
    return substr(str_shuffle($charset), 0, 12);
}

// INSIDE THE LOOP
$generated_pass = genpass();
echo "<td>$generated_pass</td>";

Something like that.

Danny
  • 1,185
  • 2
  • 12
  • 33