Insert Data in MySQL Using PHP PDO

Insert Data in MySQL Using PHP PDO

Insert Data in MySQL Using PHP PDO  user has the power to use new features offered in the latest versions of MySQL servers. It’s possible to test if Varnish cache server is operating by running You should observe the next output. Put simply, mysql isn’t insecure, but simply more difficult to use securely in contrast to other available extensions, like mysqli and PDO. The great thing about PDO queries is you don’t really set your variables within the query, you just put some placeholders you will link to your variable in a very simple collection. You must create the database yourself manually. It’s a consistent approach to access databases. To install the newest edition of the application, we need to add additional Varnish repository, which can be readily accomplished by running the next command.


You can view another article :

U.S English Spelling Test Answer-2019

Freelancer US English Level 1 Exam (Answers)

5 Best CSS Learning Website
10 best Web Design Learning Site(Free)

Creating the database connection

For Insert Data in MySQL Using PHP PDO Open your any of text editor Then just copy/paste the code below then name it conn.php.

  $conn = new PDO( 'mysql:host=localhost;dbname=db_form', 'root', '');
    die("Fatal Error: Connection Failed!");

Creating The Form Page

Insert Data in MySQL Using PHP PDO  is wherever we will make a simple form for our application. To make the forms just copy and write it into you text editor, then save it as publicized under.


<!DOCTYPE html>
<html >
    <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  <nav class="navbar navbar-default">
    <div class="cotaniner-fluid">
      <a class="navbar-brand">Sourcecodester</a>
  <div class="col-md-3"></div>
  <div class="col-md-6 well">
    <h3 class="text-primary">PHP - Simple Submit POST Using PDO</h3>
    <hr style="border-top:1px dotted #ccc;"/>
    <button class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Member</button>
    <br /><br />
    <table class="table table-bordered">
      <thead class="alert-info">
      <tbody style="background-color:#fff;">
            require_once 'conn.php';
            $sql = "SELECT * FROM `member`";
            $query = $conn->prepare($sql);
            while($fetch = $query->fetch()){
          <td><?php echo $fetch['firstname']?></td>
          <td><?php echo $fetch['lastname']?></td>
          <td><?php echo $fetch['age']?></td>
          <td><?php echo $fetch['address']?></td>
  <div class="modal fade" id="form_modal" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <form method="POST" action="save_member.php">
          <div class="modal-header">
            <h3 class="modal-title">Add Member</h3>
          <div class="modal-body">
            <div class="col-md-2"></div>
            <div class="col-md-8">
              <div class="form-group">
                <input type="text" name="firstname" class="form-control" required="required"/>
              <div class="form-group">
                <input type="text" name="lastname" class="form-control" required="required" />
              <div class="form-group">
                <input type="number" name="age" class="form-control" min="0" max="200" required="rquired" />
              <div class="form-group">
                <input type="text" name="address" class="form-control" required="required"/>
          <div style="clear:both;"></div>
          <div class="modal-footer">
            <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
            <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
<script src="js/jquery-3.2.1.min.js"></script>	
<script src="js/bootstrap.js"></script>	

Creating the Core Task

This code covers the main function of the application. This code will store the data inputs using PDO request. To do that inscribe these block of codes inside the text editor and save it as save_member.php

  require_once 'conn.php';
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $age = $_POST['age'];
    $address = $_POST['address'];
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $sql = "INSERT INTO `member`(firstname, lastname, age, address)  VALUES ('$firstname', '$lastname', '$age', '$address')";
    }catch(PDOException $e){
      echo $e->getMessage();
    $conn = null;
    header("location: index.php");



Check Also

100+ Top Free High PR Classified Submission Sites List UK

100+ Top Free High PR Classified Submission Sites List UK

High PR Classified submission sites list uk. The commercials were initially introduced in newspapers and …

Leave a Reply

Translate »