1/12/19

PHP & MySQli Coding References - Part 2 | insert data's


This is my part -2 php mysql tutorial. If you haven't seen PHP & MySQli Coding References - Part 1 | Connect database | view data's, just watch it.

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

About

Hi, I'm Najathi.
I've started entrepreneurial company, Twin Brothers.Inc.
One is self-funded & the other is venture backed. I also send a weekly. where I share relevent, curated links.

Every Week I Publish a short post on writing, publishing, or content of IT Related

Contact Form

Name

Email *

Message *