2013-02-20 61 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

相关问题