Ok. Today I'm going to talk about how to insert the data in php using mysqli
6. How to insert the data in the database
(We have two pages)
1. index.php (it is a interface)
2. process.php (it is a part of coding file)
Step 1: (index.php)
<?php //Database Connection ?>
<?php $mysqli= new mysqli('localhost','root','','crud') or die(mysqli_error($mysqli)); ?>
<?php //Includes process.php file ?>
<?php require_once 'process.php'; ?>
<div class="row justify-content-center">
<form action="process.php" method="POST">
<div class="form-group">
<label>Name:</label>
<input type="text" name="name" class="form-control" placeholder="Enter your name"/>
</div>
<div class="form-group">
<label>Location:</label>
<input type="text" name="location" class="form-control" placeholder="Entet your location"/>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" name="save">Save</button>
</div>
</form>
</div>
Step 2: (process.php)
<?php
$mysqli= new mysqli('localhost','root','','crud') or die(mysqli_error($mysqli));
if(isset($_POST['save'])){
$name=$_POST['name'];
$location=$_POST['location'];
$mysqli->query("INSERT INTO data (name,location) VALUES ('$name','$location')") or die(mysqli_error($mysqli));
}
?>
No comments:
Post a Comment