如何从for循环中获取生成的密码的值

时间:2013-02-20 01:59:01

标签: php

我管理我的代码,从我的记录表(“tbl_studentreg”)生成我的学生(约45名学生)的随机密码。现在我想用生成的随机密码将它保存到新表(tbl_student),但我的问题是我无法从生成的密码中获取数据..请帮助我并给我一些建议。

<?php
function genpass(){
    $charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()_+';
    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>

1 个答案:

答案 0 :(得分:0)

由于您发现了发布问题,请添加密码安全性:

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

这会为您的密码添加一些安全性