how to generate random password - php

i just want to have random password from all the query results i did of the students. on my situation, i tried to generate but it returns only 1 generated password to all queried students(about 45 students). how should i make a random one. plss help..
heres my code
<?php
if(isset($_POST['generate'])){
$charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!##$%^&*()_+';
$generated_pass = substr(str_shuffle($charset), 0, 12);
$generate = $_POST['generate'];
}
?>
<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)) {
//how could i make this one generate random password for every students..?
for($i=0; $i <= $row->studId; $i++){
echo "<td>$generated_pass</td>";
}
}
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>

function genpass(){
$charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!##$%^&*()_+';
return substr(str_shuffle($charset), 0, 12);
}
// INSIDE THE LOOP
$generated_pass = genpass();
echo "<td>$generated_pass</td>";
Something like that.

Related

How to Export MySQL Data in CSV/Excel File using PHP/MySQL upon clicking the button?

I have created sql view. I want to export the view contents to excel sheet upon clicking the button say "Export in Excel". How do I do that?. Looking forward to your help.
<?php
$db_host = '';
$db_user = '';
$db_pwd = '';
$database = '';
$table = '';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM view_abc");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<div class='wrapper'>
<font size=3 color=blue><b>Entry";
echo "</font></b><table class='table'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td><b>{$field->name}</b></td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
/* foreach($row as $cell)
echo "<td>$cell</td>";
*/
echo "<td bgcolor='#d5eaf0'>{$row[0]}</td>";
echo "<td bgcolor='#d5eaf0'>{$row[1]}</td>";
if ($row[2]==0)
echo "<td bgcolor='green'>{$row[2]}</td>";
else if ($row[2]>0)
echo "<td bgcolor='red'>{$row[2]}</td>";
if ($row[3]==0)
echo "<td bgcolor='green'>{$row[3]}</td>";
else if ($row[3]>0)
echo "<td bgcolor='red'>{$row[3]}</td>";
if ($row[4]>90)
echo "<td bgcolor='green'>{$row[4]}</td>";
else if ($row[4]<90)
echo "<td bgcolor='red'>{$row[4]}</td>";
if ($row[5]>90)
echo "<td bgcolor='green'>{$row[5]}</td>";
else if ($row[5]<90)
echo "<td bgcolor='red'>{$row[5]}</td>";
if ($row[6]>90)
echo "<td bgcolor='green'>{$row[6]}</td>";
else if ($row[6]<90)
echo "<td bgcolor='red'>{$row[6]}</td>";
if ($row[7]>90)
echo "<td bgcolor='green'>{$row[7]}</td>";
else if ($row[7]<90)
echo "<td bgcolor='red'>{$row[7]}</td>";
if ($row[8]>90)
echo "<td bgcolor='green'>{$row[8]}</td>";
else if ($row[8]<90)
echo "<td bgcolor='red'>{$row[8]}</td>";
if ($row[9]>90)
echo "<td bgcolor='green'>{$row[9]}</td>";
else if ($row[9]<90)
echo "<td bgcolor='red'>{$row[9]}</td>";
if ($row[10]=='YES')
echo "<td bgcolor='green'>{$row[10]}</td>";
else if ($row[10]=='NO')
echo "<td bgcolor='red'>{$row[10]}</td>";
if ($row[11]==0)
echo "<td bgcolor='green'>{$row[11]}</td>";
else if ($row[11]>0)
echo "<td bgcolor='red'>{$row[11]}</td>";
if ($row[12]==0)
echo "<td bgcolor='green'>{$row[12]}</td>";
else if ($row[12]>0)
echo "<td bgcolor='red'>{$row[12]}</td>";
if ($row[13]<28)
echo "<td bgcolor='green'>{$row[13]}</td>";
else if ($row[13]>28)
echo "<td bgcolor='red'>{$row[13]}</td>";
echo "<td bgcolor='#d5eaf0'>{$row[14]}</td>";
echo "</tr></div>\n";
}
mysql_free_result($result);
?>
use the below query
$res = mysql_query("SELECT * INTO OUTFILE '$name.csv' FROM users ");
here $name is the name of the file you want to create.
otherwise you can use the present date by using date function instead of a static name.
The file will be stored in xampp/mysql/data/your database name ( assuming you are using xampp).

paging is not working for jquery datatable

i am using jquery table to show table on a web page.The table is coming up properly but paging for the table and showing number of records and search options are not working for the table generated.All the records are populating at the load of the page.This is my code
PHP code
$qry = " SELECT AssetId,";
$qry .= $data;
$qry .= " from Completedetails";
mysql_select_db($database_finalkms, $finalkms);
$query_getcolumns = $qry;
$getcolumns = mysql_query($query_getcolumns, $finalkms) or die(mysql_error());
$row_getcolumns = mysql_fetch_assoc($getcolumns);
$totalRows_getcolumns = mysql_num_rows($getcolumns);
if (($getcolumns)||(mysql_errno == 0))
{
echo "<table width='50%' id='sample_2'><thead><tr>";
if (mysql_num_rows($getcolumns)>0)
{
//loop thru the field names to print the correct headers
$i = 0;
while ($i < mysql_num_fields($getcolumns))
{
echo "<th align='center'>". mysql_field_name($getcolumns, $i) . "</th>";
$i++;
}
echo "</tr></thead>";
//display the data
while ($rows = mysql_fetch_array($getcolumns,MYSQL_ASSOC))
{
echo "<tbody><tr >";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
}
}else{
echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>";
}
echo "</tbody></table>";
}else{
echo "Error in running query :". mysql_error();
}
?>
Java Script
<script>
jQuery(document).ready(function() {
// initiate layout and plugins
$("#sample_2").dataTable({"bPaginate": true});
});
</script>
<input name="hdnfld" id="hdnfld" type="hidden" value="<?php echo $qry;?>"/>
Please help me in this regard.
Try the following modified code of yours, if still not working then give the live URl where ur html output is displyed.
if (($getcolumns)||(mysql_errno == 0))
{
// Displying the headers
$i = 0;
echo "<table width='50%' id='sample_2'><thead><tr>";
while ($i < mysql_num_fields($getcolumns))
{
echo "<th align='center'>". mysql_field_name($getcolumns, $i) . "</th>";
$i++;
}
echo "</tr></thead>";
// Data Section
echo "<tbody>";
if (mysql_num_rows($getcolumns)>0)
{
while ($rows = mysql_fetch_array($getcolumns, MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
echo "</tr>";
}
}else{
echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>";
}
echo "</tbody></table>";
}else{
echo "Error in running query :". mysql_error();
}

Database query info to dropdown menu

I am taking info from a database query and adding it to a dropdown menu form (that is placed inside a table). The query is in a separate function that is called from within the form. It adds the info from the database to the correct location on the table, but it is not in the dropdown menu. I used variables $a, $b, and $c to test my syntax, and it works fine with those variables. Is it an issue with the function call? Any ideas?
Here is the code:
<?php
function fill_dropdown(){
include("../secure/database.php");
$conn = pg_connect(HOST." ".DBNAME." ".USERNAME." ".PASSWORD)
or die('Could not connect: ' . pg_last_error()); //error if could not connect to database
$query = "SELECT country_code, name FROM lab5.country ORDER BY name ASC";
$result = pg_query($query) or die("Unable to execute: " . pg_last_error($conn));
$numRow = 0;
//results are good so output them to HTML
//echo "test<br />";
while ($line1 = pg_fetch_array($result, null, PGSQL_ASSOC)){
$counter = 0;
//echo "test<br />";
foreach ($line1 as $col_value){ // then add all data for attributes in succeeding columns
if($counter == 0){
$code[$numRow] = $col_value;//array($numRow => $col_value);
echo "\t\t<input type=\"hidden\" name=\"code\" value=\"$code[$numRow]\" />";
//echo $code[$numRow] . "<br />";
}
elseif($counter == 1){
$country_name[$numRow] = $col_value;
echo "<option value=$country_name[$numRow]>$country_name[$numRow]</option>";
//echo $country_name[$numRow] . "<br />";
}
$counter++;
}
$numRow++;
}
//echo "end test<br />";
}
echo "<table border = \"1\">";
echo "<form method=\"POST\" action=\"exec.php\">"; //save and cancel buttons
for($i=1; $i<5; $i++) //building initial table
{
echo "\t<tr>\n";
echo "\t\t<td>";
if($i == 1)
echo "Name";
elseif($i == 2)
echo "Country Code";
elseif($i == 3)
echo "District";
else
echo "Population";
echo "</td>\n";
echo "<td>\n";
if($i == 1){
echo "<input type=\"text\" name=\"name\">";
}
elseif($i == 2){
echo "<select name=\"country_code\">"; //dropdown box
$c = 0; //these are just to show that this way works
$a = "IOT";
$b = "test2";
$numRow = 1;
echo "<option value=\"IOT\">British Indian Ocean Territory</option>";
echo "<option value=$a>$b</option>";
//echo "<option value=" . $country_name[$numRow] . ">" . $country_name[$numRow] . "</option>";
fill_dropdown();
//echo "<option value=\"Brunei\">Brunei</option>";
echo "</select>";
}
elseif($i == 3){
echo "<input type=\"text\" name=\"district\">";
}
else{
echo "<input type=\"text\" name=\"population\">";
}
}
echo "</td>";
echo "</tr>";
echo "</table>";
echo "\t\t<input type=\"submit\" value=\"Save\" name=\"save\" />";
echo "<input type=\"button\" value=\"Cancel\" onclick=\"top.location.href='" . $_SERVER['HTTP_REFERER'] . "';\" />\n";
echo "</form>";
?>
It would seem you can replace
while ($line1 = pg_fetch_array($result, null, PGSQL_ASSOC)){
$counter = 0;
//echo "test<br />";
foreach ($line1 as $col_value){ // then add all data for attributes in succeeding columns
if($counter == 0){
$code[$numRow] = $col_value;//array($numRow => $col_value);
echo "\t\t<input type=\"hidden\" name=\"code\" value=\"$code[$numRow]\" />";
//echo $code[$numRow] . "<br />";
}
elseif($counter == 1){
$country_name[$numRow] = $col_value;
echo "<option value=$country_name[$numRow]>$country_name[$numRow]</option>";
//echo $country_name[$numRow] . "<br />";
}
$counter++;
}
$numRow++;
}
with
while ($row = pg_fetch_assoc($result)) {
// why do you want this line at all?
echo "\t\t<input type=\"hidden\" name=\"code\" value=\"$row[country_code]\"/>";
echo "<option value=\"$row[name]\">$row[name]</option>";
}
The only thing I can see that is wrong, is not having quotes around the option's value attribute. I don't understand what you are expecting to achieve by interleaving hidden inputs with options, though. Judging by your $a, $b, $c template, maybe what you actually want is:
while ($row = pg_fetch_assoc($result)) {
echo "<option value=\"$row[country_code]\">$row[name]</option>";
}

php mysql only returning first record

I have this following code in a switch statement since the query can vary based on screen-name(1 word), full name (2 words...maybe 3), or the email address which "explode"'s to 3 words (I put it back together later, no biggy). However, cannot locate the problem in the following code:
$query = "SELECT * FROM personal WHERE FirstName='$searchName' OR LastName='$searchName' OR Email='$searchName' OR displayName='$searchName'";
$result = mysql_query($query) or die(mysql_error());
echo "<center><table border='1' width='70%'>";
if (mysql_num_rows($result)) {
$row = mysql_fetch_assoc($result);
$myBuddy = $row['FirstName'].
" ".$row['LastName'];
$picName = $row['FirstName'].$row['LastName'].$row['RecNum'];
if (file_exists('../members/'.$picName.
'/profile.jpg')) {
$picLine = "<img src = '../members/".$picName.
"/profile.jpg' alt='profile' height='100' width='100' />";
}
echo "<tr>";
echo "<td style='text-align:center'>".$picLine.
"</td>";
echo "<td style='text-align:center; color:red'>".$myBuddy.
"</td>";
echo "<td style='text-align:center'><input type='button' value='Add Buddy'></td>";
}
echo "</table>";
break;
Problem is, I have several people in my MySql database with the same last name (Jones), however in the code above only returns the first occurance of someone with the last name Jones. Trying to return them all, not just one. I know I am just overlooking something small & stupid - been doing php/mysql for 2 years now - never had to do a search page before. Any help is greatly appreciated.
while ($row = mysql_fetch_assoc($result)) {
//...
//use $row
}
instead of
$row = mysql_fetch_assoc($result);
You have too loop the results, like in the following example:
echo "<center><table border='1' width='70%'>";
if (mysql_num_rows($result) )
{
while($row = mysql_fetch_assoc($result)) {
$myBuddy = $row['FirstName'] . " " . $row['LastName'];
$picName=$row['FirstName'].$row['LastName'].$row['RecNum'];
if (file_exists('../members/' . $picName . '/profile.jpg'))
{
$picLine = "<img src = '../members/" . $picName .
"/profile.jpg' alt='profile' height='100' width='100' />";
}
echo "<tr>";
echo "<td style='text-align:center'>" . $picLine . "</td>";
echo "<td style='text-align:center; color:red'>" . $myBuddy . "</td>";
echo "<td style='text-align:center'><input type='button' value='Add Buddy'></td>";
}
echo "</table>";
break;
}
Above, I have used a while loop.
while($row = mysql_fetch_assoc($result)) {
// code
}

how to get the value of generated password from the for loop

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.
<?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>
Since you figured out the posting problem, add password security:
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;
}
This will add some security to your passwords

Categories