{"id":1822,"date":"2017-09-14T23:35:25","date_gmt":"2017-09-14T18:05:25","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=1822"},"modified":"2026-09-15T17:36:20","modified_gmt":"2026-09-15T12:06:20","slug":"update-query-php-using-function","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/update-query-php-using-function\/","title":{"rendered":"How to Update MySQL Records in PHP Using a Custom Function"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Updating existing records is a common requirement when working with PHP and MySQL databases. Instead of writing the same update logic repeatedly, you can create a custom PHP function that accepts the table, data, and conditions required for the update operation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we will explain how to update a product record in a MySQL database using PHP and MySQLi. We will create a product table, display the existing product details in an edit form, and use a custom function to execute the update query.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before proceeding, ensure that you have:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>A PHP-enabled web server.<\/li>\n\n\n\n<li>MySQL or MariaDB installed and running.<\/li>\n\n\n\n<li>Basic knowledge of PHP and MySQL.<\/li>\n\n\n\n<li>A database named <code>demo<\/code>.<\/li>\n\n\n\n<li>MySQLi support enabled in PHP.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create the Product Table<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a table named <code>product<\/code> using the following SQL query:<\/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<p class=\"wp-block-paragraph\">The table contains product information such as the product ID, name, price, image, and category ID.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create the HTML Edit Form<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create an edit form to display and update the product details.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, define the database connection details and retrieve the product record:<\/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=\"\">define(\"DB_HOST\", \"localhost\");\ndefine(\"DB_USER\", \"root\");\ndefine(\"DB_PSSWD\", \"\");\ndefine(\"DB_NAME\", \"demo\");\n\n$id = 1;\n\n$sql = \"SELECT * FROM `product` WHERE pid=$id\";\n\n$result = mysqli_query($conn, $sql);\n\n\/\/ Associative array\n$row = mysqli_fetch_assoc($result);\n\n\/\/ print_r($row);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create the HTML form:<\/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=\"editproduct-form\">\n\n    &lt;label for=\"cat-id\">Select Category&lt;\/label>\n    &lt;input name=\"cat_id\" id=\"cat-id\" value=\"\">\n\n    &lt;label for=\"product-name\">Product Name&lt;\/label>\n    &lt;input\n        id=\"product-name\"\n        name=\"product_name\"\n        placeholder=\"Enter Category\"\n        value=\"\"\n        type=\"text\"\n    >\n\n    &lt;label for=\"product-price\">Product Price&lt;\/label>\n    &lt;input\n        id=\"product-price\"\n        name=\"product_price\"\n        placeholder=\"Enter Price\"\n        value=\"\"\n        type=\"text\"\n    >\n\n    &lt;input type=\"hidden\" name=\"id\" id=\"id\" value=\"\">\n\n    &lt;button\n        type=\"submit\"\n        name=\"save\"\n        class=\"btn btn-success\"\n        value=\"save\"\n        id=\"add-save\"\n    >\n        Save\n    &lt;\/button>\n\n&lt;\/form><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The hidden <code>id<\/code> field is used to identify the product record that needs to be updated.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Create the Update Function<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a custom function named <code>qry_update()<\/code> to perform the update operation:<\/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=\"\">function qry_update($table = '', $data = '', $where = '')\n{\n    define(\"DB_HOST\", \"localhost\");\n    define(\"DB_USER\", \"root\");\n    define(\"DB_PSSWD\", \"\");\n    define(\"DB_NAME\", \"demo\");\n\n    $conn = mysqli_connect(\n        DB_HOST,\n        DB_USER,\n        DB_PSSWD,\n        DB_NAME\n    );\n\n    $cols = array();\n\n    foreach ($data as $key => $val) {\n        $cols[] = \"$key = '$val'\";\n    }\n\n    $query = \"UPDATE $table SET \" . implode(', ', $cols);\n\n    if (!empty($where)) {\n        foreach ($where as $key => $value) {\n            $where_array[] = $key . ' = \"' . $value . '\"';\n        }\n\n        $query .= \" WHERE \" . implode(' AND ', $where_array);\n    }\n\n    \/\/ echo $query;\n\n    $result = mysqli_query($conn, $query);\n\n    if ($result) {\n        echo 1;\n    } else {\n        echo 0;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The function accepts three parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$table<\/code> \u2013 The name of the database table.<\/li>\n\n\n\n<li><code>$data<\/code> \u2013 The column names and values that need to be updated.<\/li>\n\n\n\n<li><code>$where<\/code> \u2013 The condition used to identify the record to update.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Process the Form Submission<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When the user submits the form, collect the submitted values and pass them to the update 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=\"\">if (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    $where = array(\n        'pid' => $_POST['id']\n    );\n\n    $qry = qry_update('product', $data, $where);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the <code>$data<\/code> array contains the values that need to be updated, while the <code>$where<\/code> array identifies the product record using its <code>pid<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The function is then called as:<\/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_update('product', $data, $where);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This generates and executes the corresponding SQL <code>UPDATE<\/code> query.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Update Process Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The overall process can be summarized as follows:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Retrieve the existing product record from the database.<\/li>\n\n\n\n<li>Display the product details in an HTML form.<\/li>\n\n\n\n<li>Submit the updated values through the form.<\/li>\n\n\n\n<li>Create a <code>$data<\/code> array containing the updated values.<\/li>\n\n\n\n<li>Create a <code>$where<\/code> array containing the product ID.<\/li>\n\n\n\n<li>Pass the table name, data, and condition to the custom update function.<\/li>\n\n\n\n<li>Execute the MySQL <code>UPDATE<\/code> query.<\/li>\n\n\n\n<li>Return the result of the update operation.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A custom PHP function can simplify repetitive database update operations by allowing the table name, update values, and conditions to be passed as parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, we created a product table, prepared an edit form, created a custom <code>qry_update()<\/code> function, and used the function to update product information in the MySQL database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. What is an UPDATE query in MySQL?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An <code>UPDATE<\/code> query is used to modify existing records in a database table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/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=\"\">UPDATE product\nSET pname = 'Product Name', price = 100\nWHERE pid = 1;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Why use a custom update function in PHP?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A custom function allows the update logic to be reused for different tables and records instead of writing the complete update query repeatedly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. What does the <code>$where<\/code> parameter do?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>$where<\/code> parameter specifies which database record should be updated. In this example, the product ID is used:<\/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=\"\">$where = array(\n    'pid' => $_POST['id']\n);<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. Which PHP extension is used in this example?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The example uses the <strong>MySQLi<\/strong> extension to establish the database connection and execute the query.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. Is this approach suitable for production applications?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The basic concept can be used, but the original implementation should be improved for production by using prepared statements, input validation, secure database credentials, and proper error handling.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/dynamic-dependent-drop-list-using-htmlphpmysqlajax\/\">Dynamic Dependent drop down list using HTML, PHP, MySQL, and Ajax<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-change-mysql-user-authentication-plugin-for-password\/\">How to Change MySQL User Authentication Plugin for Password?<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Connect with Our Technology Experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Have a technology challenge or looking for the right solution for your business? Our team can help you with <strong>cloud, DevOps, development, infrastructure, design, and more<\/strong>. Feel free to reach out to our experts <a href=\"https:\/\/pheonixsolutions.com\/contact\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Updating existing records is a common requirement when working with PHP and MySQL databases. Instead of writing the same [&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":[179,273],"class_list":["post-1822","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-mysql-2","tag-php","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-to","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1822","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=1822"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1822\/revisions"}],"predecessor-version":[{"id":11789,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1822\/revisions\/11789"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=1822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=1822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=1822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}