How to Insert Data Into Database in PHP using Xampp

Hello php programmers, in this insert data into phpmyadmin mysql database in php using xampp example; I will teach you step by step on how to insert html form data into mysql database in php using xampp server.

How to Insert Data in MySQL using PHP Form

Inserting data into database from html form using xampp server in PHP is very easy. Let’s use the below given steps to insert form data into MySQL database in php code using xampp:

  • Step 1 – Start Apache Web Server
  • Step 2 – Create PHP Project
  • Step 3 – Create phpmyadmin MySQL Database Connection File
  • Step 4 – Create Insert Data Html Form
  • Step 5 – Create Insert Form Data PHP File
  • Step 6 – Test Insert Data PHP App

Step 1 – Start Apache Web Server

Now, you need to start your apache web server. And as well as start Apache and mysql web server. You can see in the image below how to start apache and mysql server:

start xampp apache mysql server
start xampp apache mysql server

Step 2 – Create PHP Project

In step 2, navigate to your xampp/htdocs/ directory. And inside xampp/htdocs/ directory, create one folder. And you can name this folder anything.

Here, I will “demo” the name of this folder. Then open this folder in any text editor (i will use sublime text editor).

Step 3 – Create phpmyadmin MySQL Database Connection File

In step 3, First, you need to create database and table. So run the following sql query to create database and table:

CREATE DATABASE my_db;


CREATE TABLE `customers` (
`custId` INT(11) NOT NULL AUTO_INCREMENT,
`fname` VARCHAR(255) NULL DEFAULT NULL,
`lname` VARCHAR(255) NULL DEFAULT NULL,
`email` VARCHAR(255) NULL DEFAULT NULL,
`created` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`custId`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=1;

INSERT INTO `customers` (`custId`, `fname`, `lname`, `email`, `created`) VALUES (NULL, 'Tiago', 'Sam', '[email protected]', NULL), (NULL, 'Anil', 'Kumar', '[email protected]', NULL);

Then create a php file that named mydb.php. Which is used to connect phpmyadmin mysql database to project folder(demo).

So, now create mydb.php file and add the below given code into your file:

<?php

    $hName='localhost'; // host name

    $uName='root';   // database user name

    $password='';   // database password

    $dbName = "my_db"; // database name

    $dbCon = mysqli_connect($hName,$uName,$password,"$dbName");

      if(!$dbCon){
          die('Could not Connect MySql Server:' .mysql_error());
      }
?>

Step 4 – Create Insert Data Html Form

In step 4, create a php file that named index.php. Which is used to display form. Using this form, you can submit form to database in php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>How to Insert Form Data In Database using PHP</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>

<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="page-header">
                <h2>Insert Data into Database in PHP using Xampp</h2>
            </div>
           
            <form action="insert.php" method="post">

                <div class="form-group">
                    <label>First Name</label>
                    <input type="text" name="fname" class="form-control">
                </div>                        

                <div class="form-group">
                    <label>Last Name</label>
                    <input type="text" name="lname" class="form-control">
                </div>

                <div class="form-group">
                    <label>Email</label>
                    <input type="email" name="email" class="form-control">
                </div>

                <input type="submit" class="btn btn-primary" name="submit" value="save">
            </form>
        </div>
    </div>        
</div>

</body>
</html>

Step 5 – Create Insert Form Data PHP File

In step 5, create one file that named insert.php file. This php file code will insert/store form data into mysql database. And will display result after execute insert mysql query.

Note that, If the form data is successfully inserted / stored, it will display success message. Otherwise if there is any error, it will display error message.

So, add the below code into your insert.php file:

<?php

include 'mydb.php';

if(!isset($_POST['save']))
{    
     $fname = $_POST['fname'];
     $lname = $_POST['lname'];
     $email = $_POST['email'];
 
     $query = "INSERT INTO customers (fname,lname,email)
     VALUES ('$fname','$lname','$email')";
 
     if (mysqli_query($dbCon, $query)) {
        echo "Html form data has been inserted successfully !";
     } else {
        echo "Error: " . $sql . ":-" . mysqli_error($conn);
     }
     mysqli_close($conn);
}
?>

Step 6 – Test Insert Data PHP App

Finally, you need to open your browser and type http://localhost/demo/ this run project on local machine.

Recommended PHP Tutorials

Leave a Comment