{"id":26270,"date":"2022-12-16T18:18:33","date_gmt":"2022-12-16T12:48:33","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/"},"modified":"2022-12-16T18:18:33","modified_gmt":"2022-12-16T12:48:33","slug":"solved-auto-insert-into-mysql-data-base","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/","title":{"rendered":"[Solved] Auto Insert into MySql Data Base"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-26099715\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"26099715\" data-parentid=\"26097942\" data-score=\"0\" 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>This is quite simple:<\/p>\n<p>1st what you want to do is to query the database for the previous registration no \/ID.<\/p>\n<p>something like this:<\/p>\n<pre><code>$query = mysqli_query($connect,'SELECT regCode From data ORDER BY regCode DESC LIMIT 1');\n<\/code><\/pre>\n<p>then you check if there is any result like this:<\/p>\n<pre><code>$results = count(mysqli_num_rows($query));\n<\/code><\/pre>\n<p>if it $results returns 0 (it is a fresh database) do this:<br \/>\n<code>$regCode=\"A0001\";<\/code><\/p>\n<p>else:<\/p>\n<p>create an array of alphabets.<\/p>\n<p><code>$alphabet = ['B','C','D','E']; #from B to Z (A is omitted because it is the default prefix)<\/code><\/p>\n<p>retrieve previous regCode like this:<\/p>\n<pre><code>list($regCode) = mysqli_fetch_row($query);\n<\/code><\/pre>\n<p>From here seperate the alphabet from the numbers<\/p>\n<pre><code>$prefix = substr($regCode,0,1);  \/\/get prefix\/alphabet\n$storage_id = (int) substr($regCode,1,4) +1; \/\/get code and add 1\n<\/code><\/pre>\n<p>Now test for the limit which in our case is 1000 (or any number you want):<\/p>\n<pre><code>$limit = 1000\n\nif($storage_id&gt;$limit){\n    $storage_id= '0001'; \/\/if limit exceeded reset storage id\n    $index = array_search($prefix,$alphabet); \/\/get alphabet\/prefix index in the $alphabet array\n\n    if($index&gt;=0){\n        $prefix = $alphabet[$index+1]; \/\/Get next alphabet\n    }\n}else{\n    \/\/when limit is not exceeded do this\n    if(strlen($store)!=4){ \/\/check the length of our regCode (should be 4 letters in this case if not add some zeros)\n        $storage_id = str_pad($storage_id,4,'0',STR_PAD_LEFT);\n    }\n}\n\n$regCode = $prefix.$store; \/\/combine alphabet to new storage_id\necho $regCode;\n<\/code><\/pre>\n<p>And that&#8217;s it !!!<\/p>\n<p>This is the full code. Just copy this:<\/p>\n<pre><code>&lt;?php\nmysql_connect('localhost','root','');\nmysql_select_db('test');\n\nif(isset($_REQUEST['submit']))\n{\n$query = mysql_query('SELECT regno from details ORDER BY regno DESC LIMIT 1');\n$results = mysql_num_rows($query);\nif($results==0){\n    $regCode=\"A0001\";\n    mysql_query(\"INSERT INTO details (regno) VALUES('{$regCode}')\");\n}else{\n    list($regCode) = mysql_fetch_row($query);\n    $alphabet = array('B','C','D','E');\n    $prefix = substr($regCode,0,1);  \/\/get prefix\/alphabet\n    $storage_id = (int) substr($regCode,1,4) +1;\n\n    $limit = 1000;\n\nif($storage_id&gt;$limit){\n    $storage_id= '0001'; \/\/if limit exceeded reset storage id\n    $index = array_search($prefix,$alphabet); \/\/get alphabet\/prefix index in the $alphabet array\n\n    if($index&gt;=0){\n        $prefix = $alphabet[$index+1]; \/\/Get next alphabet\n    }\n}else{\n    \/\/when limit is not exceeded do this\n    if(strlen($storage_id)!=4){ \/\/check the length of our regCode (should be 4 letters in this case if not add some zeros)\n        $storage_id = str_pad($storage_id,4,'0',STR_PAD_LEFT);\n    }\n}\n$regCode = $prefix.$storage_id; \/\/combine alphabet to new storage_id\nmysql_query(\"INSERT INTO details (regno) VALUES('{$regCode}')\");\n    }\necho $regCode;\n}\n?&gt;\n&lt;form name=\"form\" id=\"form\" method=\"post\"&gt;\n&lt;input type=\"submit\" name=\"submit\"&gt;\n&lt;\/form&gt;\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">3<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Auto Insert into MySql Data Base <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] This is quite simple: 1st what you want to do is to query the database for the previous registration no \/ID. something like this: $query = mysqli_query($connect,&#8217;SELECT regCode From data ORDER BY regCode DESC LIMIT 1&#8242;); then you check if there is any result like this: $results = count(mysqli_num_rows($query)); if it $results returns 0 &#8230; <a title=\"[Solved] Auto Insert into MySql Data Base\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/\" aria-label=\"More on [Solved] Auto Insert into MySql Data Base\">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":[339],"class_list":["post-26270","post","type-post","status-publish","format-standard","hentry","category-solved","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Auto Insert into MySql Data Base - 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-auto-insert-into-mysql-data-base\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Auto Insert into MySql Data Base - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] This is quite simple: 1st what you want to do is to query the database for the previous registration no \/ID. something like this: $query = mysqli_query($connect,&#039;SELECT regCode From data ORDER BY regCode DESC LIMIT 1&#039;); then you check if there is any result like this: $results = count(mysqli_num_rows($query)); if it $results returns 0 ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-16T12:48:33+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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Auto Insert into MySql Data Base\",\"datePublished\":\"2022-12-16T12:48:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/\"},\"wordCount\":107,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"php\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/\",\"name\":\"[Solved] Auto Insert into MySql Data Base - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-16T12:48:33+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Auto Insert into MySql Data Base\"}]},{\"@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=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Auto Insert into MySql Data Base - 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-auto-insert-into-mysql-data-base\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Auto Insert into MySql Data Base - JassWeb","og_description":"[ad_1] This is quite simple: 1st what you want to do is to query the database for the previous registration no \/ID. something like this: $query = mysqli_query($connect,'SELECT regCode From data ORDER BY regCode DESC LIMIT 1'); then you check if there is any result like this: $results = count(mysqli_num_rows($query)); if it $results returns 0 ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/","og_site_name":"JassWeb","article_published_time":"2022-12-16T12:48:33+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Auto Insert into MySql Data Base","datePublished":"2022-12-16T12:48:33+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/"},"wordCount":107,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["php"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/","url":"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/","name":"[Solved] Auto Insert into MySql Data Base - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-16T12:48:33+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-auto-insert-into-mysql-data-base\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Auto Insert into MySql Data Base"}]},{"@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=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","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\/26270","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=26270"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/26270\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=26270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=26270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=26270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}