2013-02-20 124 views
0

我管理我的代碼以從我的記錄表(「tbl_studentreg」)生成我的學生(約45名學生)的隨機密碼。現在我想用生成的隨機密碼將它保存到一個新表(tbl_student),但我的問題是我無法從生成的密碼獲取數據..請幫助我,並給我一些建議。如何從for循環獲取生成的密碼的值

<?php 
function genpass(){ 
    $charset = '[email protected]#$%^&*()_+'; 
    return substr(str_shuffle($charset), 0, 12); 
} 
?> 
<?php 
if(isset($_POST['generate'])){ 
    $generated_pass = $_POST['generate']; 
    genpass(); 
} 
?> 
<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)) { 
        for($i=0; $i <= $row->studId; $i++){ 
         $generated_pass = genpass($i);    
         echo "<td>$generated_pass</td>"; 
        } 
       } 
       if(isset($_POST['save'])) { 
//here i want to pass the value of my generated pass, 
//i use global $generated_pass but still dont work.  
        $save = $_POST['save']; 
        $insert = $mysqli->query("INSERT INTO tbl_student 
         (studId, fname, lname, mname, password, dob, address, 
          f_fname, f_mname, f_lname, m_fname, m_mname, m_lname, departmentId) 
        VALUES ('".$row->studId."', '".$row->fname."', '".$row->lname."', 
         '".$row->mname."', '$generated_pass', '".$row->dob."', '".$row->address."', 
         '".$row->f_fname."', '".$row->f_mname."', '".$row->f_lname."', 
         '".$row->m_fname."', '".$row->m_mname."', '".$row->m_lname."', 
         '".$row->departmentId."')"); 
       } 
       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> 
<form action="enroll_student.php" method="post" name="save"> 
    <input type='submit' name='save' value='Save'/> 
</form> 
+2

您存儲的密碼打開文本?你不應該那樣做 – Andrew 2013-02-20 02:03:43

+0

當你運行你的代碼時會發生什麼?你試圖找出哪個部分不工作? – octern 2013-02-20 02:10:58

+0

@Andrew ..是它的開放文本。我只是這樣做,爲了讓我知道我的功能是否正常工作..但後來如果我有我想要的所有成功,我會排除「回聲」​​$ generated_pa​​ss「;」在我的網頁,並保存爲md5 .. – 2013-02-20 02:11:17

回答

0

既然你想出張貼問題,增加密碼的安全性:

function genpass($userEmail){ 
    $charset = '[email protected]#$%^&*()_+'; 
    $pss= substr(str_shuffle($charset), 0, 12); 
    YourFunctionSendEmailWithPassword(userEmail, $pss); //email password to user 
    $salt = sha1(md5(pss)); 
    $password = md5(pss . $salt); 
    return $password; 
} 

這將增加一些安全性的密碼

+0

@andrew ... thnks給出一個想法..我只是用這個''.md5($ generated_pa​​ss)包裹我的密碼。''保存在我的表中。還增加了一個想法,將這些密碼發送給用戶。 thnks很多.. – 2013-02-21 02:22:37

相關問題