0

Good afternoon,

I don't know if I just can't do this, or if I can, and I have it structured incorrectly.

What I'm trying to to is to generate a member table from a couple of imported excel spreadsheets.

To create the member records, I must assign a 'Username' for each individual member, which I am randomly generating.

but at the same time I need to make sure that I don't randomly generate any duplicate Usernames.

I was thinking I could do this, but can't seem to get it straight in my mind, and hoping someone could point me in the right direction...

$sql_s = "
SELECT name, address, city, state, zip, phone, email
FROM import_purchase_members
ORDER BY name
";
$result_s=mysql_query($sql_s);
while($request_s=mysql_fetch_array($result_s)){
    //  Split name into first and last
    $name_parts = explode(" ", $request_s[0]);

// this is the part that is in question   
    // generate username
    $username=genPswd();// function that creates 8 uppercase Alpha
    //  ### If a duplicate is created, generate a new username
    while $username=(SELECT mem_id FROM members WHERE user = '$username';){
        $username=genPswd();
    }

    // generate password
    $pass=random_string();
    $sql_i = "INSERT INTO members(mem_id, user, pass, pass2, email, status, company, fname, lname, addr1, addr2, city, state, zip, country, phone, cell, dsp_pref, join_date, last_update, prev_email, mem_status, sq_id, sq_answer, acceptance, bypass_purchase, skype) VALUES('', '$username', '" . md5($pass) . "', '', '$request_s[6]', 'I', '', '$name_parts[0]', '$name_parts[1]', '$request_s[1]', '', '$request_s[2]', '$request_s[3]', '$request_s[4]', '', '$request_s[5]', '', '1', now(), now(), '', 'F', 1, 'none', 'N', 'N', '')";
    mysql_query($sql_i);

Any help would be greatly appreciated.

Douglas

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by showman13
0

You can't do what you are trying to do with a while loop as for one you need to include everything after while and before { in brackets but as far as I am aware you cannot select within a while condition either.

All you need to do is run another query:

$sql = mysql_query("SELECT mem_id FROM members WHERE user = '$username'");
$query = mysql_fetch_array($sql);
if ($query) {
  $username = genPswd();
}

However you will need to keep running this until you generate a username that is not already in the database which is not productive so I would recommend creating usernames that are more random or use something from the selected user data to add to the beginning or end of the random username (like the id or initials etc).

0

You can't do what you are trying to do with a while loop as for one you need to include everything after while and before { in brackets but as far as I am aware you cannot select within a while condition either.

All you need to do is run another query:

$sql = mysql_query("SELECT mem_id FROM members WHERE user = '$username'");
$query = mysql_fetch_array($sql);
if ($query) {
  $username = genPswd();
}

However you will need to keep running this until you generate a username that is not already in the database which is not productive so I would recommend creating usernames that are more random or use something from the selected user data to add to the beginning or end of the random username (like the id or initials etc).

Thank you for your response...

I came to the conclusion that I couldn't do what I was trying, but hadn't decided on the best course of action...

Sounds like your solution might just do the trick...

Thanks again
Douglas

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.