how to get the value of generated password from the for loop
<p>i manage my codes to generate a random password of my students(about 45 students)  from my records table("tbl_studentreg"). now  i want to save it to a new table(tbl_student) with the generated random password, but my problem is i couldnt get the data from the generated password.. pls help me and give me some advice.</p>

<pre><code>&lt;?php
function genpass(){
    $charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&amp;*()_+';
    return substr(str_shuffle($charset), 0, 12);
}
?&gt;
&lt;?php
if(isset($_POST['generate'])){
    $generated_pass = $_POST['generate'];
    genpass();
}
?&gt;
&lt;form method="post" action='enroll_student.php' name ='register'&gt;

    &lt;?php
    $yr = date("Y");
    if ($result = $mysqli-&gt;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-&gt;num_rows &gt; 0)
        {
            echo "&lt;table width= '1000'&gt;";
            echo "&lt;tr&gt;&lt;th&gt;StudentID&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Date of Birth&lt;/th&gt;&lt;th&gt;Address&lt;/th&gt;&lt;th&gt;Department&lt;/th&gt;&lt;th&gt;School Year&lt;/th&gt;&lt;th&gt;Password&lt;/th&gt;&lt;/tr&gt;";

            while ($row = $result-&gt;fetch_object())
            {
                echo "&lt;tr&gt;";

                echo "&lt;td align='center'&gt;" . $row-&gt;studId . "&lt;/td&gt;";  
                echo "&lt;td align='center'&gt;" . $row-&gt;fname . " ". $row-&gt;mname ." ". $row-&gt;lname ." &lt;/td&gt;";
                echo "&lt;td align='center'&gt;".$row-&gt;dob."&lt;/td&gt;";
                echo "&lt;td align='center'&gt;" . $row-&gt;address. "&lt;/td&gt;";
                echo "&lt;td align='center'&gt;".$row-&gt;departmentName."&lt;/td&gt;";
                echo "&lt;td align='center'&gt;".$row-&gt;sy."&lt;/td&gt;";

                if(isset($generated_pass)) {
                    for($i=0; $i &lt;= $row-&gt;studId; $i++){ 
                        $generated_pass = genpass($i);             
                        echo "&lt;td&gt;$generated_pass&lt;/td&gt;";
                    }
                }
                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-&gt;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-&gt;studId."', '".$row-&gt;fname."', '".$row-&gt;lname."', 
                        '".$row-&gt;mname."', '$generated_pass', '".$row-&gt;dob."', '".$row-&gt;address."',
                        '".$row-&gt;f_fname."', '".$row-&gt;f_mname."', '".$row-&gt;f_lname."', 
                        '".$row-&gt;m_fname."', '".$row-&gt;m_mname."', '".$row-&gt;m_lname."', 
                        '".$row-&gt;departmentId."')");
                }
                echo "&lt;/tr&gt;";
            }
            echo "&lt;/table&gt;";
        }
        else
        {
            echo "No Results.";
        }
    }
    else
    {
        echo "Error: " . $mysqli-&gt;error;
    }
    $mysqli-&gt;close();
    echo '&lt;br&gt;';
include 'count.php'; //this one will give the total no. of results, just ignore.
?&gt;

&lt;br /&gt;        

&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;input type='submit' name='generate' value='Generate'/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;form action="enroll_student.php" method="post" name="save"&gt;
    &lt;input type='submit' name='save' value='Save'/&gt;
&lt;/form&gt;
</code></pre>
