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 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了