In this tutorial I am going to show you how to insert data in mysql using php form. This is a basic tutorial for every PHP beginner because in almost every PHP application there is form. So I am going to create one simple form with two fields. I will not apply any PHP validation in this tutorial. It is just a simple insertion of data.
First make sure you have xampp or wampp on your system. I am personally using xampp in my windows 7 machine. If you don’t have xampp in your machine. Check my post how to install xampp on local machine.
Step 1: Create Database
First you have to create database in your phpmyadmin. If you are running xampp on default ports then your phpmyadmin address should be http://localhost/phpmyadmin
. In this tutorial I am creating database name as demo
Step 2: Create Table
In step 2, you need to create table in demo database. Table name must be posts with 4 fields i.e. id, post_title, post_content, created.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
CREATE TABLE `posts` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `post_title` VARCHAR(255) NULL DEFAULT NULL, `post_content` TEXT NULL, `created` DATETIME NULL DEFAULT NULL, PRIMARY KEY (`id`) ) COLLATE='latin1_swedish_ci' ENGINE=InnoDB AUTO_INCREMENT=8 ; |
Step 3: Create Database Connection
After completion of first 2 steps, 3rd step is to create database connection in file. mysqli_connect()
function is used to establish a connection with mysql database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $host = "localhost"; $user = "root"; $pass = ""; $db = "demo"; $conn = mysqli_connect($host,$user,$pass, $db); if(!$conn) { die("Connection failed: " . mysqli_connect_error()); } ?> |
Step 4: Form HTML
In this step I will create 1 input field name as title and one textarea name as post_content. Both fields have required attribute which means form will not save until title and post_content have any text. In form tag action attribute is empty (action=””) which means form will be posted on the same page and method=”post” which means data is sensitive and will not show in url. For more details on form post method visit POST(HTTP).
1 2 3 4 5 6 7 8 9 10 |
<div class="container"> <h3>Add Post</h3> <form action="" method="post"> <input type="text" name="title" placeholder="Title of the post" required> <textarea cols="40" placeholder="Post Content" rows="8" name="post_content" required></textarea> <button type="submit" name="submit">Submit</button> </form> </div> |
Step 5: Apply CSS
In this step I add some css to the html tags for better look.
1 2 3 4 5 6 7 8 |
body{font-family:verdana;} .container{width:500px;margin: 0 auto;} h3{line-height:20px;font-size:20px;} input{display:block;width:350px;height:20px;margin:10px 0;} textarea{display:block;width:350px;margin:10px 0;} button{background:green; border:1px solid green;width:70px;height:30px;color:#ffffff} |
Step 6: Get Title and Post Content values and save them in mysql
After form submission I get title and post_content values and save them in $postTitle
and $postContent
variable. $created
is using a php built-in date function which returns date and time. $sql
is a mysql insert statement. $result
is using mysqli_query
function which takes 2 paramter first one is database connection and second is sql query. mysqli_query()
function runs sql statement. If data will save then it will print “Post has been saved successfully“. Otherwise it will print “Unable to save post”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
if(isset($_POST['submit'])) { $postTitle = $_POST['title']; $postContent = $_POST['post_content']; $created = date("Y-m-d H:i:s"); $sql = "insert into posts (post_title, post_content, created) values ('".$postTitle."', '".$postContent."', '".$created."')"; $result = mysqli_query($conn, $sql); if($result) { echo "Post has been saved successfully"; } else { echo "Unable to save post"; } } |
Step 7: All together
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<?php $host = "localhost"; $user = "root"; $pass = ""; $db = "demo"; $conn = mysqli_connect($host,$user,$pass, $db); if(!$conn) { die("Connection failed: " . mysqli_connect_error()); } if(isset($_POST['submit'])) { $postTitle = $_POST['title']; $postContent = $_POST['post_content']; $created = date("Y-m-d H:i:s"); $sql = "insert into posts (post_title, post_content, created) values ('".$postTitle."', '".$postContent."', '".$created."')"; $result = mysqli_query($conn, $sql); if($result) { echo "Post has been saved successfully"; } else { echo "Unable to save post"; } } ?> <!DOCTYPE html> <html> <head> <title> How to insert data in mysql using php</title> <style> body{ font-family:verdana; } .container{width:500px;margin: 0 auto;} h3{line-height:20px;font-size:20px;} input{display:block;width:350px;height:20px;margin:10px 0;} textarea{display:block;width:350px;margin:10px 0;} button{background:green; border:1px solid green;width:70px;height:30px;color:#ffffff} </style> </head> <body> <div class="container"> <h3>Add Post</h3> <form action="" method="post"> <input type="text" name="title" placeholder="Title of the post" required> <textarea cols="40" placeholder="Post Content" rows="8" name="post_content" required></textarea> <button type="submit" name="submit">Submit</button> </form> </div> </body> </html> |