user3272367
user3272367

Reputation: 69

Generate random string in php/mysql

I need to generate a random string when I insert data in my mysql. I did read about uuid or cast(rand) but I cant find anything that looks like I can use it.

My data comes from a app.

I made a new row called code and made it unik.

I hope you can help me :)

how do I tell my insert to generate a random string to my row code?

// array for JSON response
$response = array();

 // check for required fields
if (isset($_POST['name']) && isset($_POST['nummer']) && isset($_POST['description']) && isset($_POST['dato'])) {

$name = $_POST['name'];
$nummer = $_POST['nummer'];
$description = $_POST['description'];
$dato = $_POST['dato'];

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

mysql_set_charset("utf8");

// mysql inserting a new row
$result = mysql_query("INSERT INTO products(name, nummer, description, dato) VALUES('$name', '$nummer', '$description', '$dato')");

Ok this is what I got so far

if (isset($_POST['name']) && isset($_POST['nummer']) && isset($_POST['description']) && isset($_POST['dato'])) {

$name = $_POST['name'];
$nummer = $_POST['nummer'];
$description = $_POST['description'];
$dato = $_POST['dato'];
// $code = $_POST['code'];

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

  function generate_random_string($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
  }


// mysql inserting a new row
$result = mysql_query("INSERT INTO products(name, nummer, description, dato, code) VALUES('$name', '$nummer', '$description', '$dato', '$randomString')");

But I dont get anything in my code row?

    return $randomString;

$random_str = generate_random_string(10);

}


// mysql inserting a new row
$result = mysql_query("INSERT INTO products(name, nummer, description, dato, code) VALUES('$name', '$nummer', '$description', '$dato', '$random_str')");

Upvotes: 2

Views: 3173

Answers (2)

teeyo
teeyo

Reputation: 3755

You can generate a random number using PHP's function rand :

// code = rand($min , $max); for example
code = rand(100000, 999999); // will generate a number between 100000 and 999999
// then add the column to your insert if it belongs to the same table or 
// make another query to insert the code if it belongs to a different table

note that you can use the current time and md5 function to make stronger unique codes.

Also, if you like to take this logic to your database, try to create a trigger that will runs after each insert.

Good luck.

Upvotes: 2

Touhid
Touhid

Reputation: 659

You can try the code below:

    function generate_random_string($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;
    }

echo generate_random_string(15);  // define amount of string length in parametre

Source: PHP random string generator

If you use random number as Random String code is below:

$number = range(100000,999999);
shuffle($number);
$ran_string = $number[0];
echo $ran_string;

Upvotes: 1

Related Questions