Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Into Multiple times

Status
Not open for further replies.

ja01

Technical User
Dec 14, 2005
48
US
$sql = "insert into admin values ('','test1','test1',password('test83'))";

What I am doing here is inserting a last name, user id and password(hashed) into a table. The first '' is autoincremented. I have 100 dfferent names, user id and passwords to insert. How can I write this to include all 100. Will I need to write one insert statement, upload it 100 times. I would not mind uploading a .txt file, exported from a access table, but how would I handle the hash password
 
here is some (undebugged) code for a read-write from a csv file and also an equivalent code snip for a plain write from a multidimensional array - you don't articulate the curent form of the input data.

Code:
$n = 100 ; //number of lines to insert
$i = 0;
$values = "";
# there is no reason why this could not be read directly from a csv

/*====csv code read
$fh = fopen(filename, "rb");
while (($array = fgetcsv($fh, 10000, ",")) !== FALSE):
$values .= "(".$array[0] .", ". $array[1] . ", ". hsh($array[2] . "), "; //assuming the csv is set out as lastname, userid, password
endwhile;


$values = rtrim ($values, ","); // strip the final trailing comma

$sql = "Insert into admin (`lastname`, `userid`, `pwd`) VALUES " . $values;

//do the database insert here.  you do not need to insert the autoincrement if this is set as a primary key.  mysql will handle this for you.

*/

##if the input is in some other form (not a csv)
# assume all the insert values are held in an array called $array with a shape or recordnumber= array(fields...)
while ($i< $n):
$values .= "(".$array[$i]['lastname'] .", ". $array[$i]['userid'] . ", ". hsh($array[$i]['password'] . "), ";
$i++;
endwhile;

function hsh($str)
{`
   return md5($str); //or whatever hashing function you use. i personally don't use the mysql password function.
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top