{"id":32740,"date":"2023-02-01T11:39:31","date_gmt":"2023-02-01T06:09:31","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/"},"modified":"2023-02-01T11:39:31","modified_gmt":"2023-02-01T06:09:31","slug":"solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/","title":{"rendered":"[Solved] How to Create an auto-increment value on MySQL using a text field? [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-71646248\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"71646248\" data-parentid=\"71646002\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>There are many things to solve the problem. Let me show you one of them by using MySQL procedures.<\/p>\n<p>Steps are as follows:<\/p>\n<p>1\u00b0) create a procedure that generates an identifier for a table (generate_id)<\/p>\n<p>2\u00b0) create a procedure that inserts the data into the table (insert_users) by using the first procedure (generate_id) to get a formatted ID, then it will return the inserted ID as a SELECT query.<\/p>\n<p>3\u00b0) Now, call the inserting procedure (insert_users)<\/p>\n<p>For more information, chat with <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/facebook.com\/adam.saozer\">Michel Magloire Ekanga<\/a> who is the main creator of this craftiness.<\/p>\n<p>LET&#8217;S USE AN EXAMPLE FOR EACH STEP<\/p>\n<h2>STEP 1<\/h2>\n<p>generate_id procedure that should take as parameters: table_name, the primary_key, a joiner or a prefix for ID, the length for ID, and the output<\/p>\n<p>Our result should be as below: USER20210909000002 (joiner=USER, year=2021, mounth=09, day=09, increment=000002)<\/p>\n<pre><code># ---------------------   DEFINITION ------------------------------\nDROP PROCEDURE IF EXISTS generate_id;\nDELIMITER $$\n\nCREATE PROCEDURE generate_id(IN _db_table VARCHAR(255), IN _pkey VARCHAR(255),IN _joiner VARCHAR(255),_length INT, OUT _new_id VARCHAR(255))\nBEGIN\n    SET @max_id = NULL;\n    SET @sql = CONCAT('select max(`', _pkey, '`)  into @max_id from `', _db_table, '`');\n    PREPARE stmt FROM @sql;\n    EXECUTE stmt;\n    DEALLOCATE PREPARE stmt;\n    #------- Length for identifier -------\n    SET @length = 10;\n    SET @joiner_len = CHAR_LENGTH(_joiner);\n    IF _length &gt; 0 AND (_length - CHAR_LENGTH(_joiner)) &gt;= 10 THEN\n    SET @length = _length;\n    END IF;\n    #----------------------------\n    #--- the date variables ---\n    SET @today = DATE_FORMAT(NOW(),'%Y%m%d');\n    SET @r_str = LPAD(1,(@length - (8 + @joiner_len)), '0');\n    #\n    #---- FORMATING ID ------------------------\n    #\n    IF @max_id IS NULL THEN \n    # the table is empty\n    SET _new_id = CONCAT(_joiner,@today,@r_str);\n    ELSE \n    # the table is not empty\n    # 1\u00b0) reading parts from previous ID \n    SET @strlen = CHAR_LENGTH(@max_id);\n    SET @old_r_str = SUBSTR(@max_id, (@joiner_len + 4 + 2 + 2 + 1), @strlen);\n    SET @old_idx = CONVERT(@old_r_str, SIGNED INTEGER);\n    # 2\u00b0) checking if dates are the same\n    SET @old_date = SUBSTR(@max_id, (@joiner_len + 1), 8);\n    -- dates are not the same, we just take the 8 characters for date from the field\n    SET @new_idx = 1;\n    SET @new_r_str = LPAD(@new_idx,(@length - (8 + @joiner_len)), '0');\n    SET @new_max = CONCAT(_joiner,@today,@new_r_str);\n    IF @today = @old_date THEN\n        SET @new_idx = @old_idx + 1;\n        SET @new_r_str = LPAD(@new_idx,(@length - (8 + @joiner_len)), '0');\n        SET @new_max = CONCAT(_joiner,@old_date,@new_r_str);\n    END IF;\n    SET _new_id =  @new_max;\n    END IF;\nEND;\n$$\n\nDELIMITER ;\n<\/code><\/pre>\n<h2>STEP 2<\/h2>\n<p>Procedure that uses the generated ID for insertion, we don&#8217;t need to provide an ID, it will generate the ID automaticaly<\/p>\n<pre><code>DROP PROCEDURE IF EXISTS insert_users;\nDELIMITER $$\n\nCREATE PROCEDURE insert_users(\n    login_user VARCHAR(255),\n    pass_user VARCHAR(255)\n)\nBEGIN\n    CALL generate_id('users', 'row_id', 'USR', 22, @new_id);\n    SET @last_inserted_id = @new_id;\n    SET @sql = CONCAT(\"INSERT INTO users(row_id, login_user, pass_user) VALUES ('\", \n        @last_inserted_id, \"','\",\n        login_user, \"','\",\n        pass_user, \"')\"\n    );\n    PREPARE stmt FROM @sql;\n    EXECUTE stmt;\n    DEALLOCATE PREPARE stmt;\n\n    # ---- export the new ID before exiting the function\n    SELECT @last_inserted_id AS lastInsertId;\n\nEND;\n$$\n\nDELIMITER ;\n<\/code><\/pre>\n<h2>STEP 3<\/h2>\n<p>How to use with PHP, for example<\/p>\n<pre><code>&lt;?php\n\nfunction insert(){\n    $db = $pdo; \/\/ I am using PDO as driver, renewed as Class\n    $lastInsert = null;\n    \/\/---------------\n    $sql = \"CALL insert_users(?,?)\";\n    \/\/---------------\n    \n    $req = $db-&gt;prepare($sql);\n    $req-&gt;execute($login_user,$pass_user);\n    if($req-&gt;rowCount() &gt; 0){\n        $lastInsert = $req-&gt;fetch()-&gt;lastInsertId;\n        return true;\n    }else{\n        return false;\n    }\n}\n\n?&gt;\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How to Create an auto-increment value on MySQL using a text field? [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] There are many things to solve the problem. Let me show you one of them by using MySQL procedures. Steps are as follows: 1\u00b0) create a procedure that generates an identifier for a table (generate_id) 2\u00b0) create a procedure that inserts the data into the table (insert_users) by using the first procedure (generate_id) to &#8230; <a title=\"[Solved] How to Create an auto-increment value on MySQL using a text field? [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/\" aria-label=\"More on [Solved] How to Create an auto-increment value on MySQL using a text field? [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[340,339],"class_list":["post-32740","post","type-post","status-publish","format-standard","hentry","category-solved","tag-mysql","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How to Create an auto-increment value on MySQL using a text field? [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How to Create an auto-increment value on MySQL using a text field? [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] There are many things to solve the problem. Let me show you one of them by using MySQL procedures. Steps are as follows: 1\u00b0) create a procedure that generates an identifier for a table (generate_id) 2\u00b0) create a procedure that inserts the data into the table (insert_users) by using the first procedure (generate_id) to ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-01T06:09:31+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How to Create an auto-increment value on MySQL using a text field? [closed]\",\"datePublished\":\"2023-02-01T06:09:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/\"},\"wordCount\":203,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"mysql\",\"php\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/\",\"name\":\"[Solved] How to Create an auto-increment value on MySQL using a text field? [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-02-01T06:09:31+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How to Create an auto-increment value on MySQL using a text field? [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How to Create an auto-increment value on MySQL using a text field? [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How to Create an auto-increment value on MySQL using a text field? [closed] - JassWeb","og_description":"[ad_1] There are many things to solve the problem. Let me show you one of them by using MySQL procedures. Steps are as follows: 1\u00b0) create a procedure that generates an identifier for a table (generate_id) 2\u00b0) create a procedure that inserts the data into the table (insert_users) by using the first procedure (generate_id) to ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/","og_site_name":"JassWeb","article_published_time":"2023-02-01T06:09:31+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How to Create an auto-increment value on MySQL using a text field? [closed]","datePublished":"2023-02-01T06:09:31+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/"},"wordCount":203,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["mysql","php"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/","name":"[Solved] How to Create an auto-increment value on MySQL using a text field? [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-02-01T06:09:31+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-create-an-auto-increment-value-on-mysql-using-a-text-field-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How to Create an auto-increment value on MySQL using a text field? [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/32740","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=32740"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/32740\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=32740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=32740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=32740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}