How to insert data into the database using PHP

0
2130
How to insert data

Once you’re a new learner in PHP, you may don’t know how to insert data into the database. On the other hand if you’re a programming student you many want to know about how to insert data in the database. In this article, I will show you how to insert data to database. I will use MySQL database for this article. Let’s start…

What is Database?

When you want to learn about how to insert data in to the database, you have known about database first. Database is the storage system where you can store your website or software all data like texts and images or any other files. You can take those files or texts when you need. Normally, we see two type of data in the websites or software. We see files and texts. So, we have known that database is storage where we can store our software or website data and access any time. Database is located on server. You have to buy hosting for server from any provider like Namecheap. But when you’re a learner, you can install some software for creating server in local computer.

How to setup server in local computer?

If you want to setup server in local computer you can use Xampp. Xampp is open source and very easy to use. You need to install xampp software in your local computer as usual. Once you have install xampp in your local computer you will be find a new directory in your C://Program Files > xampp.

It’s the root folder for your server. But when you will be use server just start Apache and MySQL from the xampp control panel.

Let’s create a form in HTML

When you want to insert some data using any programming language you have create an HTML form. So that, users can input there information which will be stored on the database. I have created a simple HTML form using following code:

<form action=” “ >
	<label>Your Name</label>
	<input placeholder=”Your Name” name=”user_name” />

	<label>Your Email</label>
	<input placeholder=”Your Email” name=”user_email” />

	<label>Your Mobile</label>
	<input placeholder=”Your Mobile” name=”user_number” />

	<input value=”Submit” name=”submit_form” />
</form>

Create a database and database table on your server.

In this stage, I will write “localhost/phpmyadmin” in my browser. Because of that, I’m using Xampp as my server. Now I will be create a database which name is “demo”. Finally I will create a table which will be “users” named. Table column will be following named:

  • sl (it will be auto increment)
  • name (it will be various character)
  • email (it will be various character)
  • phone (it will be various character)

Let’s connect the database

Now we will write code for connect into the database. You need to place following code in the header of PHP file which we have created before.

<?php
$host = "localhost";
$db = "demo";
$user = "root";
$pass = ""
$con = new mysqli_connect($host, $user, $pass, $db);

if ($con->connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}

If you don’t see any error, you’re all set. Now you’re ready to insert data into the database.

Insert data using HTML form.

Now we’re all set to insert data into the database. You need to put following codes into the PHP file where we have created an HTML form.

<?php
if(isset($_POST['submit_form'])){
// Cache all input values
 $name = isset($_POST['user_name']) ? $_POST['user_name'] : NULL;
 $email = isset($_POST['user_email']) ? $_POST['user_email'] : NULL;
 $mobile = isset($_POST['user_number']) ? $_POST['user_number'] : NULL;

 $sql = "INSERT INTO users (name, email, phone) VALUES ('$name', '$email', '$mobile')";

if ($con->query($sql) === TRUE) {
  echo "User information inserted successfully";
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

}

If you have done all things properly. Then you will be seen success message when you will be submitted the form.

Conclusion

If you don’t see the success message, check the database first. Double-check your writings. Because of that many times, we mistake in code writings. You may like my other article about Best Content Management System for PHP Programming.