How to update table column with random numbers in php

Hello everyone.Is there any way to update the unique_id inside userprofile table with random numbers ranging from 1 to 1000000000?

Here are the codes that we are using. The problem is, it generates uniform numbers. We need to have it different or unique. Our system has almost 10000 users.

We are updating our database since we have added another feature. So, we need to insert unique_id to each user for them to get access to the new feature. The codes below works fine but its functionality will take time for us to update each user. Can anyone help us revise these codes to automatically update the unique_id column in one click on a button?


include 'database/dbconfig.php';


    $new_id = $_POST['new_id'];

    $ran_num = rand(1, 10000000000);

    $user_id = $ran_num+$new_id;

    $query = "UPDATE userprofile SET unique_id = ? WHERE user_id = ?";

    $stmt = $connection->prepare($query);

    $stmt->bind_param('si', $user_id, $new_id);


    header ('Location: update_all');


$sql = "SELECT * FROM userprofile WHERE unique_id = '' ";

$query_run = mysqli_query($connection,$sql);

if($row = mysqli_fetch_assoc($query_run)){

    while($row = mysqli_fetch_array($query_run))



<form action="" method="POST">

    <input type="text" value="<?php echo $row['user_firstname'];?>">

    <input type="text" value="<?php echo $row['unique_id']?>">

    <input type="text" name="new_id" value="<?php echo $row['user_id']?>">

    <button type="submit" name="updateBtn">UPDATE</button>


    <?php }}


Is there any reason you can’t just use an auto-increment column?

Look into using uuids; you’re pretty likely to get a collision if you uses the number system you’re talking about.

Sponsor our Newsletter | Privacy Policy | Terms of Service