{"id":1775,"date":"2017-07-11T18:25:22","date_gmt":"2017-07-11T12:55:22","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=1775"},"modified":"2026-09-07T17:43:48","modified_gmt":"2026-09-07T12:13:48","slug":"insert-query-php-using-function","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/insert-query-php-using-function\/","title":{"rendered":"Insert Query in PHP Using Function"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In PHP, data can be inserted into a MySQL database using an <code>INSERT<\/code> query. When database operations are used repeatedly, creating a custom function can make the code easier to manage and reuse. This article explains how to create a custom PHP function for inserting data into a MySQL table and how to use it with an HTML form to insert product details.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before proceeding with this example, the following basic requirements are needed:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Basic knowledge of PHP<\/li>\n\n\n\n<li>Basic understanding of HTML forms<\/li>\n\n\n\n<li>Basic knowledge of MySQL<\/li>\n\n\n\n<li>PHP and MySQL environment<\/li>\n\n\n\n<li>A MySQL database<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create a Basic HTML Page with Form<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a basic HTML form to enter the product category, product name, and product price.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;form method=\"POST\" action=\"\" id=\"addproduct-form\">\n\n&lt;label for=\"hd-desc\">Select Category&lt;\/label>\n\n&lt;select name=\"cat_id\" id=\"cat-id\">\n&lt;option value=\"\" selected=\"\">Select Category&lt;\/option>\n&lt;option value=\"8\">mobile&lt;\/option>\n&lt;option value=\"9\">Tv&lt;\/option>\n&lt;option value=\"10\">XYZ&lt;\/option>\n&lt;option value=\"11\">mobile2&lt;\/option>\n&lt;option value=\"12\">Tv2&lt;\/option>\n&lt;option value=\"13\">Tv 3&lt;\/option>\n&lt;\/select>\n\n&lt;label for=\"hd-desc\">Product Name&lt;\/label>\n\n&lt;input id=\"product-name\" name=\"product_name\" placeholder=\"Enter Category\" value=\"\" type=\"text\">\n\n&lt;label for=\"hd-desc\">Product Price&lt;\/label>\n\n&lt;input id=\"product-price\" name=\"product_price\" placeholder=\"Enter Price\" value=\"\" type=\"text\">\n\n&lt;!--   &lt;input type=\"submit\" name=\"save\"> -->\n\n&lt;button type=\"submit\" name=\"save\" class=\"btn btn-success\" value=\"save\" id=\"add-save\">Save&lt;\/button>\n\n&lt;\/form><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create PHP Connection String, Query Insert Function and Form Submit Action<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create the PHP database connection and custom insert function.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\ndefine(\"DB_HOST\", \"localhost\");\n\ndefine(\"DB_USER\", \"root\");\n\ndefine(\"DB_PSSWD\", \"\");\n\ndefine(\"DB_NAME\", \"test\");\n\n$conn = mysql_connect(DB_HOST,DB_USER,DB_PSSWD);\n\nmysql_select_db(DB_NAME, $conn);\n\nfunction qry_insert($table, $data)\n{\n    $fields = array_keys($data);  \n\n    $values = array_map(\"mysql_real_escape_string\", array_values($data));\n\n    return mysql_query(\"INSERT INTO $table(\".implode(\",\", $fields).\") VALUES ('\".implode(\"','\", $values).\"');\") or die(mysql_error());\n}\n\nif(isset($_POST['save']))\n{\n    $data = array(\n        'cat_id' => $_POST['cat_id'],\n        'pname' => $_POST['product_name'],\n        'price' => $_POST['product_price']\n    );\n\n    $qry = qry_insert('product',$data);\n}\n\n?><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Create the Product Table<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a table named <code>product<\/code> using the following code.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE TABLE `product` (\n  `pid` int(11) NOT NULL AUTO_INCREMENT,\n  `pname` varchar(100) NOT NULL,\n  `price` int(11) NOT NULL,\n  `pimg` varchar(100) NOT NULL,\n  `cat_id` int(11) NOT NULL,\n  PRIMARY KEY (`pid`)\n) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Create the Final Code<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you following above step you get final code like this.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\ndefine(\"DB_HOST\", \"localhost\");\n\ndefine(\"DB_USER\", \"root\");\n\ndefine(\"DB_PSSWD\", \"\");\n\ndefine(\"DB_NAME\", \"karthik\");\n\n$conn = mysql_connect(DB_HOST,DB_USER,DB_PSSWD);\n\nmysql_select_db(DB_NAME, $conn);\n\nfunction qry_insert($table, $data)\n{\n    $fields = array_keys($data);  \n\n    $values = array_map(\"mysql_real_escape_string\", array_values($data));\n\n    return mysql_query(\"INSERT INTO $table(\".implode(\",\", $fields).\") VALUES ('\".implode(\"','\", $values).\"');\") or die(mysql_error());\n}\n\nif(isset($_POST['save']))\n{\n    $data = array(\n        'cat_id' => $_POST['cat_id'],\n        'pname' => $_POST['product_name'],\n        'price' => $_POST['product_price']\n    );\n\n    $qry = qry_insert('product',$data);\n}\n\n?><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">HTML<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!DOCTYPE html>\n\n&lt;html>\n\n&lt;head>\n\n&lt;title>Insert function&lt;\/title>\n\n&lt;\/head>\n\n&lt;body>\n\n&lt;form method=\"POST\" action=\"\" id=\"addproduct-form\">\n\n&lt;label for=\"hd-desc\">Select Category&lt;\/label>\n\n&lt;select name=\"cat_id\" id=\"cat-id\">\n\n&lt;option value=\"\" selected=\"\">Select Category&lt;\/option>\n\n&lt;option value=\"8\">mobile&lt;\/option>\n\n&lt;option value=\"9\">Tv&lt;\/option>\n\n&lt;option value=\"10\">XYZ&lt;\/option>\n\n&lt;option value=\"11\">mobile2&lt;\/option>\n\n&lt;option value=\"12\">Tv2&lt;\/option>\n\n&lt;option value=\"13\">Tv 3&lt;\/option>\n\n&lt;\/select>\n\n&lt;label for=\"hd-desc\">Product Name&lt;\/label>\n\n&lt;input id=\"product-name\" name=\"product_name\" placeholder=\"Enter Category\" value=\"\" type=\"text\">\n\n&lt;label for=\"hd-desc\">Product Price&lt;\/label>\n\n&lt;input id=\"product-price\" name=\"product_price\" placeholder=\"Enter Price\" value=\"\" type=\"text\">\n\n&lt;!--   &lt;input type=\"submit\" name=\"save\"> -->\n\n&lt;button type=\"submit\" name=\"save\" class=\"btn btn-success\" value=\"save\" id=\"add-save\">Save&lt;\/button>\n\n&lt;\/form>\n\n&lt;\/body>\n\n&lt;\/html><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How It Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The HTML form collects the category, product name, and product price.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the <strong>Save<\/strong> button is clicked, the submitted values are stored in the <code>$data<\/code> array. The <code>qry_insert()<\/code> function receives the table name and data array and creates the <code>INSERT<\/code> query.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The data is then inserted into the <code>product<\/code> table using <code>mysql_query()<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, a custom PHP function is used to insert product details into a MySQL database. The function accepts the table name and data as parameters, allowing the insert query to be created using the provided values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is the purpose of the <code>qry_insert()<\/code> function?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>qry_insert()<\/code> function is a custom function used to insert data into a database table.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Which table is used in this example?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The example uses the <code>product<\/code> table.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What product details are inserted?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The category ID, product name, and product price are inserted into the table.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How is the insert function called?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The function is called using:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$qry = qry_insert('product',$data);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/install-apache2-php7-mysql-redhat-7-server\/\">How to Install Apache, PHP, and MySQL on Red Hat 7?<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-install-mysql-8-0-on-centos-7\/\">How to install MySQL 8.0 on CentOS 7<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to Our Technology Experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Planning your AWS infrastructure or looking to improve an existing cloud environment? Our team can help with cloud architecture, infrastructure, DevOps, security, deployment, and ongoing optimization.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><a href=\"https:\/\/pheonixsolutions.com\/contact\">Connect with our technology experts.<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In PHP, data can be inserted into a MySQL database using an INSERT query. When database operations are used [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[273],"class_list":["post-1775","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-php","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-sD","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=1775"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1775\/revisions"}],"predecessor-version":[{"id":11484,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1775\/revisions\/11484"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=1775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=1775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=1775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}