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 phpmyadmin mysql database in php using xampp.

Also i will guide you how to write clean php code for inserting data into phpmyadmin database from form.

Prerequisite for how to store form data in database using php

You must have installed XAMPP server on your window, ubuntu or Mac machine. And a text editor that wrote code in your php files.

How to Insert Data in MySQL using PHP Form

You can follow the below given each steps to insert form data into phpmyadmin database in php using xampp:

start xampp apache mysql server
start xampp apache mysql server

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:

Step 2 – Create Project Folder

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 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 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);
}
?>

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *