コードを管理して、記録テーブル ("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>