{"id":22613,"date":"2024-08-19T11:45:02","date_gmt":"2024-08-19T10:45:02","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/como-realizar-una-insercion-de-datos-en-mysql-utilizando-php\/"},"modified":"2025-10-12T21:00:25","modified_gmt":"2025-10-12T20:00:25","slug":"como-realizar-una-insercion-de-datos-en-mysql-utilizando-php","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/how-to-insert-data-in-mysql-using-php\/","title":{"rendered":"How to perform a data insertion in MySQL using PHP"},"content":{"rendered":"<p>Inserting data is one of the fundamental operations when working with databases. In this article, I will show you how to perform a data insertion into MySQL using PHP. <\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-insert-data-in-mysql-using-php\/#%C2%BFQue_es_PHP_y_MySQL\" >What is PHP and MySQL?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-insert-data-in-mysql-using-php\/#Paso_1_Establecer_conexion_con_la_base_de_datos\" >Step 1: Establish connection to the database<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-insert-data-in-mysql-using-php\/#Paso_2_Crear_la_consulta_de_insercion\" >Step 2: Create the insert query<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-insert-data-in-mysql-using-php\/#Paso_3_Ejecutar_la_consulta_de_insercion\" >Step 3: Run the insert query<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-insert-data-in-mysql-using-php\/#Conclusion\" >Conclusion<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-insert-data-in-mysql-using-php\/#Preguntas_frecuentes\" >Frequently asked questions<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-insert-data-in-mysql-using-php\/#1_%C2%BFQue_es_PHP\" >1. What is PHP?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-insert-data-in-mysql-using-php\/#2_%C2%BFQue_es_MySQL\" >2. What is MySQL?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-insert-data-in-mysql-using-php\/#3_%C2%BFQue_funcion_se_utiliza_para_establecer_una_conexion_con_la_base_de_datos_en_PHP\" >3. What function is used to establish a connection with the database in PHP?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/nelkodev.com\/en\/blog\/how-to-insert-data-in-mysql-using-php\/#4_%C2%BFComo_se_ejecuta_una_consulta_de_insercion_en_PHP\" >4. How do you execute an insert query in PHP?<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_PHP_y_MySQL\"><\/span>What is PHP and MySQL?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Before we begin, it is important to understand what PHP and MySQL are. PHP is a general-purpose programming language especially suitable for web development. On the other hand, MySQL is a very popular database management system used to store and retrieve information.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Paso_1_Establecer_conexion_con_la_base_de_datos\"><\/span>Step 1: Establish connection to the database<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Before we can perform a data insert into MySQL, we must establish a connection to the database. To do this, we will use the function <code>mysqli_connect()<\/code> of PHP. This function allows us to connect to a MySQL server by specifying the server address, username, password and database name.<\/p>\n<p>Here is an example of how to establish a connection to the database:<\/p>\n<pre><code>&lt;?php\n$servername = &quot;localhost&quot;;\n$username = &quot;root&quot;;\n$password = &quot;contrase\u00f1a&quot;;\n$dbname = &quot;basededatos&quot;;\n\n$conn = mysqli_connect($servername, $username, $password, $dbname);\n\nif (!$conn) {\n    die(&quot;Error al conectarse a la base de datos: &quot; . mysqli_connect_error());\n}\necho &quot;Conexi\u00f3n exitosa&quot;;\n?&gt;<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Paso_2_Crear_la_consulta_de_insercion\"><\/span>Step 2: Create the insert query<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Once we have established the connection to the database, we can proceed to create the insert query. In this query, we will specify the table in which we want to insert the data and the values we want to insert in each column.<\/p>\n<p>Here is an example of how to create an insert query:<\/p>\n<pre><code>&lt;?php\n$sql = &quot;INSERT INTO usuarios (nombre, email, telefono) VALUES (&#039;John Doe&#039;, &#039;johndoe@example.com&#039;, &#039;123456789&#039;)&quot;;\n\nif (mysqli_query($conn, $sql)) {\n    echo &quot;Datos insertados correctamente&quot;;\n} else {\n    echo &quot;Error al insertar datos: &quot; . mysqli_error($conn);\n}\n\nmysqli_close($conn);\n?&gt;<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Paso_3_Ejecutar_la_consulta_de_insercion\"><\/span>Step 3: Run the insert query<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Once we have created the insert query, we can execute it using the function <code>mysqli_query()<\/code> of PHP. This function takes two parameters: the database connection and the insert query.<\/p>\n<p>If the data insertion is successful, the function <code>mysqli_query()<\/code> will return true. Otherwise it will return false and we can get more information about the error using the function <code>mysqli_error()<\/code>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In this article, we have learned how to perform data insertion in MySQL using PHP. Always remember to establish a connection to the database, create the correct insert query, and execute it properly. I hope this tutorial is useful to you!<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Preguntas_frecuentes\"><\/span>Frequently asked questions<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h4><span class=\"ez-toc-section\" id=\"1_%C2%BFQue_es_PHP\"><\/span>1. What is PHP?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>PHP is a general-purpose programming language widely used in web development.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"2_%C2%BFQue_es_MySQL\"><\/span>2. What is MySQL?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>MySQL is a very popular relational database management system.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"3_%C2%BFQue_funcion_se_utiliza_para_establecer_una_conexion_con_la_base_de_datos_en_PHP\"><\/span>3. What function is used to establish a connection with the database in PHP?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>The function <code>mysqli_connect()<\/code> It is used to establish a connection with the database in PHP.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"4_%C2%BFComo_se_ejecuta_una_consulta_de_insercion_en_PHP\"><\/span>4. How do you execute an insert query in PHP?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>An insert query is executed using the function <code>mysqli_query()<\/code> of PHP.<\/p>","protected":false},"excerpt":{"rendered":"<p>La inserci\u00f3n de datos es una de las operaciones fundamentales al trabajar con bases de datos. En este art\u00edculo, te mostrar\u00e9 c\u00f3mo realizar una inserci\u00f3n de datos en MySQL utilizando PHP. \u00bfQu\u00e9 es PHP y MySQL? Antes de comenzar, es importante entender qu\u00e9 son PHP y MySQL. PHP es un lenguaje de programaci\u00f3n de uso [&hellip;]<\/p>","protected":false},"author":1,"featured_media":22614,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420],"tags":[],"class_list":["post-22613","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/22613","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=22613"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/22613\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/22614"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=22613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=22613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=22613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}