Insert Data into mysql


Insert data into mysql 


If you are a beginner or a php proagrammer you must learn how to insert data into database using php.Here is the story for insertion data into database. First of all you require to login into your server or in your xampp and then create a database i named it as ''php operations" here and insert the database by entering the following sql commands in sql tab of mysql.For more information you may watch the following video or just copy and paste the code below .





CREATE TABLE `crud` (
`id`
int(11) NOT NULL,
`firstName`
varchar(255) NOT NULL,
`lastName`
varchar(255) NOT NULL,
`gender`
varchar(255) NOT NULL,
`age`
varchar(255) NOT NULL,
`email`
varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `crud` ADD PRIMARY KEY (`id`);
ALTER TABLE `crud`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;



Now our database is ready .I uses bootstap there for layout if you don't have knowledge of bootstrap don't worry just ignore the classes in html and concentrate on php logic.


Connect to database for insertion


After creating database our next step is to make a connection file using php so that we can connect to our database for this thing we need our localhost or server name and the username for host and password we will use mysql_select function for database selection and mysql_connect function for make sure that we have successfully connected to database. Here i am using connection.php  file for providing connection to database in index.php file.



<?php
$connection = mysqli_connect('localhost', 'root', '');
 if (!$connection){
  die("Database Connection Failed" . mysqli_error($connection));
 }
  $selectDb = mysqli_select_db($connection, 'php_operations');
 if (!$selectDb){
  die("Unable to select database" . mysqli_error($connection));
 }
?>

We are ready to connect now we will create index.php  file that will contain lot of functions as given below.

Insert data in database create index.php file


First we need connection in index file so we need require_once function than we will check if there is something coming from form i.e check if post is set and noting is empty than we will collect all form data into variables and after collection we will check for that all field have data if all field don't have data than it will display "kindly fill all fields" if all things are ready to go than we will execute a query for database insertion using mysql_query function it will return resource in result variable and display a success messege to user.


<!DOCTYPE html>
<html>
 <head>
  <title>
Data Insertion in mysql database using php</title>
   <!-- Latest bootstrap compiled and minified CSS -->
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
>
 </head>
<body>
 <div class="container">
  <div class="row">
   <form method="post" class="form-horizontal col-md-6 col-md-offset-3">
   <center><h2>Execute Insert operation</h2></center>
<br/><hr/>
    <div class="form-group">
     <label for="item1" class="col-sm-3 control-label">First Name</label>
    <div class="col-sm-9">
     <input type="text" name="f_name" class="form-control" id="item1" placeholder="Enter Your First Name" />
    </div>
  </div>

    <div class="form-group">
     <label for="item2" class="col-sm-3 control-label">Last Name</label>
    <div class="col-sm-9">
     <input type="text" name="l_name" class="form-control" id="item2" placeholder="Enter Your Last Name" />
    </div>
   </div>
    <div class="form-group">
     <label for="item3" class="col-sm-3 control-label">E-Mail</label>
    <div class="col-sm-9">
      <input type="email" name="email" class="form-control" id="item3" placeholder="Your Email" />
    </div>
    </div>
    <div class="form-group" class="radio">
     <label for="input1" class="col-sm-3 control-label">Gender</label>
    <div class="col-sm-9">
     <label>
     <input type="radio" name="gender" id="radio1" value="male" checked> Male
</label>
    <label>
     <input type="radio" name="gender" id="radios2" value="female"> Female
    </label>
   </div>
   </div>
    <div class="form-group">
     <label for="item4" class="col-sm-3 control-label">Age</label>
    <div class="col-sm-9">
     <input type="text" name="age" class="form-control" id="item4" placeholder="Your Age" />
     </div>
    </div>
     <input type="submit" class="btn btn-danger col-md-2 col-md-offset-10" value="submit" />
   </form>
   </div>
 </div>
</body>
</html>

<?php
 

 require_once ('connection.php');

  if(isset($_POST) && !empty($_POST)){
  $f_name = $_POST['f_name'];
  $l_name = $_POST['l_name'];
  $email  = $_POST['email'];
  $gender = $_POST['gender'];
  $age    = $_POST['age'];
   if(!empty($f_name) && !empty($l_name) && !empty($email )&& !empty($gender) && !empty($age)){
   $query = "INSERT INTO `crud` (firstName, lastName, email, gender, age) VALUES ('$f_name', '$l_name', '$email', '$gender', '$age')";
   $result = mysqli_query($connection, $query) or die(mysqli_error($connection));
   if($result){
    $success = "Your data has been Inserted into databse";
   }else{
   $failed = "Something went wrong please try another time";
   }
  }
  else{
    echo "<hr><div class='alert alert-danger' ><center>Kindly fill all the fields</center></div>";
 }
}
?>
<?php if(isset($success)){ ?><hr><div class="alert alert-success" ><center> <?php echo $success; ?></center> </div><?php } ?>
<?php if(isset($failed)){ ?><hr><center><div class="alert alert-danger" > <?php echo $failed; ?> </div></center><?php } ?>


If have any question kindly leave a comment in comment box.

No comments:

Post a Comment