473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Insert multiple random password into mysql

Here is my code:

[PHP]if(isset($_REQUEST['Generate']))
{
$nopwd= $_POST['nopwd'];//no of password
$length = $_POST['length'];//lenght of password
$value1= $_POST['pwdchar1'];//abcdefghijklmnopqrstuvwxyz
$value2= $_POST['pwdchar2'];//ABCDEFGHIJKLMNOPQRSTUVWXYZ
$value3= $_POST['pwdchar3'];//0123456789
$chars = $value1."".$value2."".$value3;

$max_i = strlen($chars)-1;

for($i=0;$i<$nopwd;$i++)
{
$newpwds = array();//array for random password
for($j=0;$j<$length;$j++)
{
$value[$i] .= $chars{mt_rand(0,$max_i)};
}
$newpwds[$i] = $value[$i];//insert new index into array
}
//insert into db all array index
for($x=0;$x<sizeof($newpwds);$x++)
{
mysql_query("UPDATE user SET password='".$newpwds[$x]."' WHERE accessLevel1='2' AND deptID='20'") or die(mysql_error());
}
}[/PHP]

Why all row ($nopwd) receives same random password (a value)?
Mar 13 '07 #1
2 2601
ronverdonk
4,258 Expert 4TB
You have several errors in your code. Main is that you are (a) initializing the array each time (b) replace the same chars in the array, so your passwords are always identical. The first 'for' loop must be replaced with this code.
[php] $newpwds = array(); //array for random password
for($i=0; $i<$nopwd; $i++) {
$value="";
for($j=0; $j<$length; $j++) {
$value .= $chars{mt_rand(0,$max_i)};
}
$newpwds[] = $value; //insert new index into array
}[/php]
Ronald :cool:
Mar 13 '07 #2
Thanks Ronald for ur help but i still got same output. I try to split one by one code all went ok. I expect the mysql update code not running carefully coz when i just display an array index (without include update code) it running ok.

[PHP]for($x=0;$x<sizeof($newpwds);$x++)
{
echo "Password".(1+$x)." :\t".$newpwds[$x]."<br>";
}[/PHP]
Regard,
Fareast Adam
Mar 14 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Randell D. | last post by:
Folks, I use PHP to write my form data to MySQL. I have a database with about ten tables. I'm trying to fill one table with some dummy data (its a contact manager table holding names of...
15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
7
by: RotterdamStudents | last post by:
Hello there, i have a strange problem. I can't get php to insert multiple rows at once in a MySQL database. I use the $sql = "INSERT INTO database (a,b,c,d,e) VALUES ('$a', '$b' ,'$c', '$d',...
1
by: Joe | last post by:
Hello All, I am trying to insert a record in the MS Access DB and for some reason I cannot get rid of error message, System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. ...
14
by: Ben | last post by:
I don't know whether anyone can help, but I have an odd problem. I have a PSP (Spyce) script that makes many calls to populate a database. They all work without any problem except for one...
2
by: dylanhughes | last post by:
I'm looking for an example of a login system that has multiple fields (2 to be exact) + password. e.g username, company name and password, the user, company and password are checked against a mysql...
2
by: servo | last post by:
I am using the following code. Why it does not insert record in the MySQL Table through php script: <?php $dbhost='localhost'; $dbuser='root'; $dbpass='newpass'; $mysqldb='mysql'; ...
2
by: adamace5o | last post by:
When i try to use post variables with php and mysql i can't get the insert into statement to accept varibles as values. If i use 'test' instead of $test it does work. I suspect it is something to do...
3
by: divyac | last post by:
I want to generate password randomly and send to mysql database using PHP..i have got the following function for generating password but doesn't know how to incorporate in my registration form and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.