PHP CRUD Application using functions - Insert
Hey Everyone. When developing a website using PHP, it is essential to develop it as a CRUD application.This article will show you how to insert form data into database using php with the help of functions.
First we should create our front view and the database to continue further.
<form>
<div class="form-group">
<label for="Name">Name</label>
<input type="email" class="form-control" id="Name" placeholder="Name" name="name">
</div>
<div class="form-group">
<label for="Age">Age</label>
<input type="password" class="form-control" id="Age" placeholder="Age" name="Age">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
Creating the front view
We should create a project folder in our root folder.Since I am using Wamp server, I'll create it in the www folder.
Next we should create a another folder called views to store the view files. Now I have created a php file called Home.php to create the front view.
Now my folder structure will be like ,
Now to create the view I am using Bootstrap to make it interactive. Copy the CDN link from the Bootstrap Guide and paste it in between <head> tag.
Now I will copy Form Template from the CSS in the Bootsrap site and paste it in between <body> tag.
<form>
<div class="form-group">
<label for="Name">Name</label>
<input type="email" class="form-control" id="Name" placeholder="Name" name="name">
</div>
<div class="form-group">
<label for="Age">Age</label>
<input type="password" class="form-control" id="Age" placeholder="Age" name="Age">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
Make sure that you have given a name attribute in the input tag to get values in the input field.
Creating the Database
Move in to phpmyadmin and Create the database. I have created a database called phpcrud and a table called User with 3 fields. The structure of the table is given below.
Now lets create the database connection with our project. I created a separate file called "connect.php" in my project folder and created the database connection by using the following code.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phpcrud";
// Create connection
$conn = mysqli_connect($servername, $username, $password,$dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}else{
echo "Success";
}
?>
Now run the project and check whether the database connection has been established.
Then include the connection file in the Home.php file.
<?php
require '../connect.php';
?>
In your Home.php file type the following code to call the insert function by getting the form data before starting the form.
<?php
$errname="";
$errage="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$errname = "Name is required";//validating the form
} else {
$name = mysqli_real_escape_string($conn, $_POST["Name"]);//set the form data into a variable
}
if (empty($_POST["age"])) {
$errage = "Age is required";
} else {
$age = mysqli_real_escape_string($conn, $_POST["Name"]);
}
if($errname=="" and $errage==""){
insertuser($name,$age); // call the insertuser function
}
}
?>
Now create a php file called functions.php to write the insert function. include the connect.php file in the functions.php file as we did before.Also include the functions.phpfile in the Home.php file .
Now Lets write the insert function.
<?php
require 'connect.php';
function insertuser(){
require 'connect.php';
if(isset($_POST['insert']))
{
$name = mysqli_real_escape_string($conn, $_REQUEST['name']);
$age = mysqli_real_escape_string($conn, $_REQUEST['age']);
// attempt insert query execution
$sql="INSERT INTO `user`(`UserName`, `UserAge`) VALUES ('$name','$age')";
if(mysqli_query($conn, $sql)){
// javascript to get a dialog box
echo'<script language ="javascript">';
echo'alert("User Added succesfully")';
echo'</script>';
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
}
}
?>
Now try to add data to the form. Visit my github to get a better idea.
Hope to see you soon with creating views using php functions.
Comments
Post a Comment