PHP Data Insert Into MySQL Database | Coding Cage

PHP Data Insert Into MySQL Database

By
In this post i will show you that how to insert data into mysql database , this tutorial will use php , html and mysql together and using html user friendly form that get some information and inserts the data into mysql database specified table,  using some php functions and mysql insert query it’s easy to insert data into database table , so let’s have a look.
PHP Data Insert Into MySQL Database
Here's How

1.Step
Create database and name it whatever you want
i use here

CREATE DATABASE `dbtutorials` ;

Now create table and name it ‘tbl_users’

CREATE TABLE `dbtutorials`.`tbl_users` (
`user_id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`first_name` VARCHAR( 25 ) NOT NULL ,
`last_name` VARCHAR( 25 ) NOT NULL ,
`user_email` VARCHAR( 45 ) NOT NULL
) ENGINE = InnoDB;

2.Step
html form that inserts user first name , last name and email

<form method="post">
    <table align="center">
    <tr>
    <td><input type="text" name="first_name" placeholder="First Name" required /></td>
    </tr>
    <tr>
    <td><input type="text" name="last_name" placeholder="Last Name" required /></td>
    </tr>
    <tr>
    <td><input type="text" name="city_name" placeholder="City" required /></td>
    </tr>
    <tr>
    <td><button type="submit" name="btn-save"><strong>SAVE</strong></button></td>
    </tr>
    </table>
</form>

above code will create following html form : –


3.Step
Now PHP Comes In Picture :
Connect to localhost and select the just created database.

mysql_connect("localhost","root","");
mysql_select_db("dbtutorials");
following are the variable that get the users details and stores it in php function

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$city_name = $_POST['city_name'];

sql query that insert above details in tbl_users table.

$sql_query = "INSERT INTO tbl_users(first_name,last_name,user_email) VALUES('$first_name','$last_name','$user_email')";

above two blocks are works within following condition.

if(isset($_POST['btn-save']))
{
    // functions and variables goes here
    //insert query goes here
}
here’s the query and variables function within above button save condition.

if(isset($_POST['btn-save']))
{
 // variables for input data
 $first_name = $_POST['first_name'];
 $last_name = $_POST['last_name'];
 $user_email = $_POST['user_email'];
 // variables for input data
 
 // sql query for inserting data into database
 $sql_query = "INSERT INTO tbl_users(first_name,last_name,user_email) VALUES('$first_name','$last_name','$user_email')";
 // sql query for inserting data into database
}

Here's the Complete Script :
index.php

<?php
mysql_connect("localhost","root","");
mysql_select_db("dbtutorials");
if(isset($_POST['btn-save']))
{
 // variables for input data
 $first_name = $_POST['first_name'];
 $last_name = $_POST['last_name'];
 $user_email = $_POST['user_email'];
 // variables for input data
 
 // sql query for inserting data into database
 $sql_query = "INSERT INTO tbl_users(first_name,last_name,user_email) VALUES('$first_name','$last_name','$user_email')";
 // sql query for inserting data into database
 
 // sql query execution function
 if(mysql_query($sql_query))
 {
  ?>
  <script type="text/javascript">
  alert('Data Are Inserted Successfully Checkout Your Database Table');
  </script>
  <?php
 }
 else
 {
  ?>
  <script type="text/javascript">
  alert('error occured while inserting your data');
  </script>
  <?php
 }
 // sql query execution function
}

?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Data Insert Into MySql - By Cleartuts</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>

<div id="header">
 <div id="content">
    <label>PHP Data Insert Into MySql - By Cleartuts</label>
    </div>
</div>
<div id="body">
 <div id="content">
    <form method="post">
    <table align="center">
    <tr>
    <td><input type="text" name="first_name" placeholder="First Name" required /></td>
    </tr>
    <tr>
    <td><input type="text" name="last_name" placeholder="Last Name" required /></td>
    </tr>
    <tr>
    <td><input type="text" name="city_name" placeholder="City" required /></td>
    </tr>
    <tr>
    <td><button type="submit" name="btn-save"><strong>SAVE</strong></button></td>
    </tr>
    </table>
    </form>
    </div>
</div>

<div id="footer">
 <div id="content">
    <hr /><br/>
    <label>for more tutorials and blog tips visit <a href="http://cleartuts.blogspot.com"> : cleartuts.com</a></label>
    </div>
</div>

</center>
</body>
</html>

style.css

@charset "utf-8";
/* CSS Document */
/* and last but not the least stylesheet file that makes pretty view of  */
*
{
 margin:0;
 padding:0;
}
body
{
 background:#f9f9f9;
 font-family:"Courier New", Courier, monospace;
}
#header
{
 width:100%;
 height:50px;
 background:#00a2d1;
 color:#f9f9f9;
 font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
 font-size:35px;
 text-align:center;
}
#body
{
 margin-top:80px;
}
table
{
 width:40%;
}
table,td
{
 border-collapse:collapse;
 border:solid #d0d0d0 1px;
 padding:25px;
}
table td input
{
 width:97%;
 height:35px;
 border:dashed #00a2d1 1px;
 padding-left:15px;
 font-family:Verdana, Geneva, sans-serif;
 box-shadow:0px 0px 0px rgba(1,0,0,0.2);
 outline:none;
}
table td input:focus
{
 box-shadow:inset 1px 1px 1px rgba(1,0,0,0.2);
 outline:none;
}
table td button
{
 border:solid #f9f9f9 0px;
 box-shadow:1px 1px 1px rgba(1,0,0,0.2);
 outline:none;
 background:#00a2d1;
 padding:15px;
 color:#f9f9f9;
 font-family:Arial, Helvetica, sans-serif;
 font-weight:bolder;
 border-radius:3px;
 width:100%;
}
table td button:active
{
 position:relative;
 top:1px;
}
#footer
{
 margin-top:100px;
 font-weight:bolder;
 color:#00a2d1;
 font-family:Verdana, Geneva, sans-serif;
 font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
 font-family:"Courier New", Courier, monospace;
 font-size:20px;
}
#footer a
{
 color:#004567;
 text-decoration:none;
}
#footer a:hover
{
 color:brown;
}
#footer hr
{
 width:80%;
}

That's it.



1 comment:

  1. I am new to php so i am not able to understand the coding which can brings with simple informative which is most helpful to develop the php programming skills.


    Php Training in Chennai

    ReplyDelete

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download