PHP MySQL Insert - Web Development and Web Design Codes

Latest

Monday, April 17, 2017

PHP MySQL Insert

How To Insert Data Into MySQL Table Using PHP

Insert Data Into MySQL Table Using PHP
Insert Data Into MySQL Table Using PHP
Every beginner php and MySQL learner need to learn how to insert data into MySQL database table using php..this is beginner step to start php MySQL..
In This Step We will Learn How to insert data into MySQL table.
it's very easy and Simple.just follow the below example to learn how to insert data into MySQL table..

Overview and Structure:

You will need 3 files. 
1. connect.php [To Established Connection with Database]
2. index.php [Displaying Insert Page]
3. insert.php [Inserting Data Into MySQL database]
Save all files in same folder.

Database Structure:

Insert Data PHP MySQL Database Structure
Insert Data PHP MySQL Database Structure
Database name: tutorial
Table Name: test
Creating Table "test"
CREATE TABLE IF NOT EXISTS `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(150) NOT NULL,
  `email` varchar(150) NOT NULL,
  PRIMARY KEY(id)
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=latin1;

connect.php
You have to create connect.php file to create connection between server and database..
<?php
//Creating Database connection
$conn=new mysqli('localhost','root','','tutorial');
if($conn->connect_error){
 echo("Connection Failed:".$conn->connect_error);
}
?>

'localhost' -->Server name
'root'--->mysql password
''-->mysql password
'tutorial'-->database name

index.html
in index page we will show insert form to user with submit button..


<html>
<head>
<title>Inserting Data Into Mysql using PHP</title>
</head>
<body>
<h2>Insert data into Database using PHP</h2>
<form method="POST" action="insert.php">
<table>
  <tr>
    <td>Name:</td>
 <td><input type="text" name="name" placeholder="Your Name"/></td>
  </tr>
  <tr>
    <td>Email:</td>
 <td><input type="text" name="email" placeholder="Your Email"/></td>
  </tr>
  <tr>
    <td></td>
 <td><input type="submit" name="submit" value="Insert"/></td>
  </tr>
</table>
</form>
</body>
</html>

insert.php
You have to create insert.php page to process submitted data and save into database ..
<?php
include("connect.php");
if(isset($_POST['submit'])){
  //Getting Data from index page
 $name=$_POST['name'];
 $email=$_POST['email'];
 //Checking for Empty Fields 
 if(empty($name)&&empty($email)){
  echo "Fields are mandatory||<a href=index.php>Go Back Home</a>";;
 }else{
  //Inserting Data Into MySQL Table "test" by Passing name and Email
  $result=mysqli_query($conn,"INSERT INTO test (name,email)VALUES('$name','$email')");
  //Success Massage If Data Successfully Inserted
  if($result){
   echo "Data Successfully Inserted||<a href=index.php>Go Back Home</a>";
  }else{
   //Error Message If failed to Insert Data
   echo "Sorry Failed To Inserting Data";
  }
 }
} 
?>

That's it Friends how to Insert data into database using PHP ..I hope you Enjoy This article.Please Like and Share with your Friends.
and Keep Visiting for More Tutorials.Thank you.

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.