donglinxia1541 2016-08-29 12:18 采纳率: 0%
浏览 41
已采纳

插入多个用户和随机密码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:

  • submitted user is john
  • submitted numbers is 20

Example result:

  • john1, john2... john20

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 .';';
  • 写回答

1条回答 默认 最新

  • douzhi1879 2016-08-29 13:03
    关注

    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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办