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)
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!");
<?php
$conn = new PDO( 'mysql:host=localhost;dbname=db_form', 'root', '');
if(!$conn){
die("Fatal Error: Connection Failed!");
}
?>
<?php
$conn = new PDO( 'mysql:host=localhost;dbname=db_form', 'root', '');
if(!$conn){
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.
index.php.
<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>
<table class="table table-bordered">
<thead class="alert-info">
<tbody style="background-color:#fff;">
$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="col-md-2"></div>
<input type="text" name="firstname" class="form-control" required="required"/>
<input type="text" name="lastname" class="form-control" required="required" />
<input type="number" name="age" class="form-control" min="0" max="200" required="rquired" />
<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>
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
</head>
<body>
<nav class="navbar navbar-default">
<div class="cotaniner-fluid">
<a class="navbar-brand">Sourcecodester</a>
</div>
</nav>
<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">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Address</th>
</tr>
</thead>
<tbody style="background-color:#fff;">
<?php
require_once 'conn.php';
$sql = "SELECT * FROM `member`";
$query = $conn->prepare($sql);
$query->execute();
while($fetch = $query->fetch()){
?>
<tr>
<td><?php echo $fetch['firstname']?></td>
<td><?php echo $fetch['lastname']?></td>
<td><?php echo $fetch['age']?></td>
<td><?php echo $fetch['address']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<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>
<div class="modal-body">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="form-group">
<label>Firstname</label>
<input type="text" name="firstname" class="form-control" required="required"/>
</div>
<div class="form-group">
<label>Lastname</label>
<input type="text" name="lastname" class="form-control" required="required" />
</div>
<div class="form-group">
<label>Age</label>
<input type="number" name="age" class="form-control" min="0" max="200" required="rquired" />
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="address" class="form-control" required="required"/>
</div>
</div>
</div>
<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>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="js/jquery-3.2.1.min.js"></script>
<script src="js/bootstrap.js"></script>
</body>
</html>
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
</head>
<body>
<nav class="navbar navbar-default">
<div class="cotaniner-fluid">
<a class="navbar-brand">Sourcecodester</a>
</div>
</nav>
<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">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Address</th>
</tr>
</thead>
<tbody style="background-color:#fff;">
<?php
require_once 'conn.php';
$sql = "SELECT * FROM `member`";
$query = $conn->prepare($sql);
$query->execute();
while($fetch = $query->fetch()){
?>
<tr>
<td><?php echo $fetch['firstname']?></td>
<td><?php echo $fetch['lastname']?></td>
<td><?php echo $fetch['age']?></td>
<td><?php echo $fetch['address']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<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>
<div class="modal-body">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="form-group">
<label>Firstname</label>
<input type="text" name="firstname" class="form-control" required="required"/>
</div>
<div class="form-group">
<label>Lastname</label>
<input type="text" name="lastname" class="form-control" required="required" />
</div>
<div class="form-group">
<label>Age</label>
<input type="number" name="age" class="form-control" min="0" max="200" required="rquired" />
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="address" class="form-control" required="required"/>
</div>
</div>
</div>
<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>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="js/jquery-3.2.1.min.js"></script>
<script src="js/bootstrap.js"></script>
</body>
</html>
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
if(ISSET($_POST['save'])){
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO `member`(firstname, lastname, age, address) VALUES ('$firstname', '$lastname', '$age', '$address')";
header("location: index.php");
<?php
require_once 'conn.php';
if(ISSET($_POST['save'])){
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$address = $_POST['address'];
try{
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO `member`(firstname, lastname, age, address) VALUES ('$firstname', '$lastname', '$age', '$address')";
$conn->exec($sql);
}catch(PDOException $e){
echo $e->getMessage();
}
$conn = null;
header("location: index.php");
}
?>
<?php
require_once 'conn.php';
if(ISSET($_POST['save'])){
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$address = $_POST['address'];
try{
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO `member`(firstname, lastname, age, address) VALUES ('$firstname', '$lastname', '$age', '$address')";
$conn->exec($sql);
}catch(PDOException $e){
echo $e->getMessage();
}
$conn = null;
header("location: index.php");
}
?>