kadir
kadir

Reputation: 33

Insert multiple users and random passwords Php&mysql

I am struggling to create multiple user&password and insert it in mysql column. inserting multiple username is working but not multiple random password.

mysql table "users" - columns 'user' & 'password' primary key = user column

How it works:

A user enters a username and numbers (how many user & pass to create) in an HTML form.

The code uses the submitted username and adds a serial from "1" to limit (submitted number).

Example input:

Example result:

In the future when the user requests another 10 user & pass with same name "john" the number will start from 21 (john21, john22... john30) adding another series is not done yet any help and tips are welcome.

My code:

function muser() {
    function randomPassword() {
        $alphabet = "abcdefghjkmnpqrstuwxyz23456789"; // skip 0OoIl1
        $pass = array();
        $alphaLength = strlen($alphabet) - 1; 
        for ($i = 0; $i < 6; $i++) {
            $n = rand(0, $alphaLength);
            $pass[] = $alphabet[$n];
        }
        return implode($pass); 
    }

    if(isset($_REQUEST['submit'])) {
        $user_input_user = $_REQUEST["user"];
        $user_input_limit = $_REQUEST["limit"];

        $ipass = randomPassword();
    } 

    $uiuser = $user_input_user;
    $uilimit = $user_input_limit;


    $limit = $uilimit; 
    for($x = 1; $x <= $limit; $x++) {
        $queryuser[] = "('$uiuser$x', '$ipass'),";
    }
    return implode($queryuser);
 }

 $mquery = 'INSERT INTO `users` (`user`, `pass`) VALUES ';
 $imuser = muser();
 $fquery = substr($imuser, 0, -1);
 $sql =  $mquery . $fquery .';';

Upvotes: 3

Views: 112

Answers (1)

Zenel Rrushi
Zenel Rrushi

Reputation: 2366

The problem is caused because you give value to $ipass only once.

change the for loop from this

for($x = 1; $x <= $limit; $x++) {
    $queryuser[] = "('$uiuser$x', '$ipass'),";
}

to

for($x = 1; $x <= $limit; $x++) {
    $queryuser[] = "('$uiuser$x', '$ipass'),";
    $ipass = randomPassword();
}

Upvotes: 1

Related Questions