Unique id for each row in mysqli

Ashwin Gopi Krishna

Here is the code that I've used

html:

<form action = 'insert.php' method='POST'>
<input type = "text" name = "name">
<input type = "submit" name = "submit">
</form>

php:

<?php
$servername='localhost';
$username='noob';
$password='password';
    $conn = mysqli_connect($servername, $username, $password);
    if (!$conn) 
        die("Connection failed: " . mysqli_connect_error());
$a=$_POST['name'];
mysqli_query("INSERT INTO table1 (name) VALUES ('$a')");
?>

Right now, the code above inserts a name into a table called table 1. My question now is, say I input many names into the table, but I want each name to have a unique 6 character/digit id. How would I change this code? For example, after 3 inserts, the table should look like this:

[table 1]
name               unique_id
NAME1              994323
NAME2              832344
NAME3              332123

Each value of unique_id must be unique. Is there any efficient way to do this?

user4962466

For the random code generation you can use RAND() as To obtain a random integer N in the range a <= N < b, just use the expression FLOOR(a + RAND() * (b – a)) as described in mysql docs

Your query is vulnerable to SQL Injection, fix it with mysqli_real_escape_string !

$name = mysqli_real_escape_string($_POST['name']);
$result = mysqli_query("SELECT FLOOR(100000 + RAND() * 990000) AS random_num
    FROM table1 
    WHERE random_num NOT IN (SELECT table1.code FROM table1)
    LIMIT 1");
$row = mysqli_fetch_row($result);
$code = $row[0];

mysqli_query("INSERT INTO table1 (name, code) VALUES ('$name', '$code')");

This is the working query for random digit generation: http://sqlfiddle.com/#!9/2acd3/1

EDIT:

Personally I prefer PDO instead of mysqli functions, anyway,

someone asked my why I suggest the PDO instead of MySQLi function. This is the way that I suggested, You can use whatever you want

  • Database Support

    • PDO has 12 different drivers
    • MySQLi has only Mysql driver
  • API

    • PDO: only OOP (avoiding bad practice)
    • MySQLi OOP or Procedural
  • Named parameters

    • PDO: yes
    • MySQLi no
  • Prepared statements (client side)

    • PDO: yes
    • MySQLi: no

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

fetching multiple columns of mysql table for each unique id and echoing the result in a single row of a HTML table for each unique id using PHP

From Dev

Unique row ID in R

From Dev

SELECT row with MAX id from each GROUP BY (unique_id) and ORDER BY number

From Dev

PHP / MySQLi - Catch unique ID conflict

From Dev

Query To Select Row With Specific ID For Each Of Two Unique Fields & Count Position Of Third Field

From Dev

C# Code for generating Unique ID for each row inserted into SQL Server 2008 R2

From Dev

Generate a table with <divs> where each row should contains same set of html elements but with unique id's?

From Dev

separating values by delimiter in rows PowerBI, does each row lose its Unique ID?

From Dev

Generate unique id for each device

From Dev

minimum value for each unique id

From Dev

show divs with unique id each

From Dev

Unique id for each instance of component

From Dev

PHP MySQLi Rename File to Row ID on Insert

From Dev

update mysql table row with unique row id

From Dev

MySQL: Row counter for each unique date

From Dev

Oracle Sql Statement for unique timestamp for each row

From Dev

combine row for each Unique value based on datetime

From Dev

sql - Generate a unique random value for each row

From Dev

Add a Total Row after each Unique CustomerName

From Dev

Set ID for each row in MySQL

From Dev

Uploading multiple images one to each row PHP and Mysqli db

From Dev

How to separate php mysqli result by limit each 4 result in a row

From Dev

select one radio button from each row and then submit it to database mysqli

From Dev

Add unique class to each td in each row jquery/coffeescript

From Dev

Use awk to sum or average for each unique ID

From Dev

Generate a unique link for each email id :Yii

From Dev

assigning unique ID via find() and each() iteration

From Dev

Setting unique id for each element in a dropdownlistfor (MVC)

From Dev

Pandas - Cumulative sum for each unique id

Related Related

  1. 1

    fetching multiple columns of mysql table for each unique id and echoing the result in a single row of a HTML table for each unique id using PHP

  2. 2

    Unique row ID in R

  3. 3

    SELECT row with MAX id from each GROUP BY (unique_id) and ORDER BY number

  4. 4

    PHP / MySQLi - Catch unique ID conflict

  5. 5

    Query To Select Row With Specific ID For Each Of Two Unique Fields & Count Position Of Third Field

  6. 6

    C# Code for generating Unique ID for each row inserted into SQL Server 2008 R2

  7. 7

    Generate a table with <divs> where each row should contains same set of html elements but with unique id's?

  8. 8

    separating values by delimiter in rows PowerBI, does each row lose its Unique ID?

  9. 9

    Generate unique id for each device

  10. 10

    minimum value for each unique id

  11. 11

    show divs with unique id each

  12. 12

    Unique id for each instance of component

  13. 13

    PHP MySQLi Rename File to Row ID on Insert

  14. 14

    update mysql table row with unique row id

  15. 15

    MySQL: Row counter for each unique date

  16. 16

    Oracle Sql Statement for unique timestamp for each row

  17. 17

    combine row for each Unique value based on datetime

  18. 18

    sql - Generate a unique random value for each row

  19. 19

    Add a Total Row after each Unique CustomerName

  20. 20

    Set ID for each row in MySQL

  21. 21

    Uploading multiple images one to each row PHP and Mysqli db

  22. 22

    How to separate php mysqli result by limit each 4 result in a row

  23. 23

    select one radio button from each row and then submit it to database mysqli

  24. 24

    Add unique class to each td in each row jquery/coffeescript

  25. 25

    Use awk to sum or average for each unique ID

  26. 26

    Generate a unique link for each email id :Yii

  27. 27

    assigning unique ID via find() and each() iteration

  28. 28

    Setting unique id for each element in a dropdownlistfor (MVC)

  29. 29

    Pandas - Cumulative sum for each unique id

HotTag

Archive