0

i want to create different ref_id for each users. i tried it with using str_shuffle function. it generates but when i refresh the page it changes. also, it generates the same ref_id for every user. Here are my codes:**

$users = $db->get_results("SELECT * FROM users");
if($users ){
  foreach($users as $u){
    $str = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPUQRSTUVWXYZ';
    $str_r = substr(str_shuffle($str), 0, 30);
    $ref_id=$db->query("UPDATE users SET ref_id= '$str_r'") ;
    echo "ref_id: $u->ref_id; ";
    echo '<br';
  }
}

i think i must use while but i didnt get any result. How can i fix this?**

Edit: i solved the part of "generating different id for each users" but still changes every refreshing. How can i solve that part?

here are the new version of codes above:

$users = $db->get_results("SELECT * FROM users");
if($users){
  echo '<div class="container">
         <div class="card">
           <div class="card-body">';
    foreach($users as $u){
      $str = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPUQRSTUVWXYZ';
      $str_r = substr(str_shuffle($str), 0, 20);
      $ref_id=$db->query("UPDATE users SET ref_id= '$str_r' where id='$u->id'") ;
       echo "$u->id . users ref_id: $u->ref_id; ";
       echo '<br>';

      }
    }

  echo '</div></div></div>';
  • What is a ref_id ? If it is just a random string you could simply do something like this substr(md5(rand()), 0, 30); – rich May 07 '21 at 10:40
  • ref_id is a value that i generate and write in the ref_id column in database. my function generates the value but when i refresh the page it changes. i want that ref_id wont be changed.also each user should have different ref_id but in my function, they have the same value. – Safiye Akgün May 07 '21 at 11:57
  • 1
    On the subject of generating random codes, you should note another answer I wrote: https://stackoverflow.com/questions/4570980/generating-a-random-code-in-php/64472183#64472183 . Note in particular that `str_shuffle` and `rand` do not use a secure random generator and can thus generate predictable codes. – Peter O. May 07 '21 at 12:18

0 Answers0