2013-02-19 91 views
1

我只是想從我所做的學生的所有查詢結果中隨機輸入密碼。在我的情況下,我試圖產生,但它只返回1個生成的密碼給所有查詢的學生(約45名學生)。我應該如何做一個隨機的。 PLSS幫助.. 繼承人我的代碼如何生成隨機密碼

<?php 
    if(isset($_POST['generate'])){ 
    $charset = '[email protected]#$%^&*()_+'; 
    $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> 
+1

參考[this](http://stackoverflow.com/questions/1837432/how-to-generate-random-password-with-php?rq=1)和[this](http://stackoverflow.com/questions/6101956/generate-a-random-password-in-php?rq = 1)。 – 2013-02-19 06:22:18

+0

您需要使密碼生成器成爲一個函數,併爲您需要的每個新密碼調用它。您只是一次創建它並反覆使用同一個。 – Danny 2013-02-19 06:23:51

+0

你的密碼只產生一次 – fefe 2013-02-19 06:26:44

回答

1
function genpass(){ 
$charset = '[email protected]#$%^&*()_+'; 
    return substr(str_shuffle($charset), 0, 12); 
} 

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

類似的東西。

+0

@丹尼...... thnks很多..我得到了我需要的東西。 – 2013-02-20 01:39:43