In the early posts, we do not save the order data. To deliver the user
order, we have to save the order data permanently. To do so we will need
suitable
database server. I will use MySQL.
What is MySQL?
MySQL is a
Relational Database Management System.
It can store, search, sort and retrieve data efficiently. You can use
it freely under the GPL license. You can check more about MySQL in its
official website www.mysql.com
What do we need?
In this series, we will learn about MySQL by using phpMyAdmin. It will make you more easy to
create a database
by using phpMyAdmin than command line because it is a GUI(Graphical
User Interfaces) system. To use phpMyAdmin you don't need to install
anything if you have installed xampp or wamp as mentioned in this post.
Type this address "http://localhost/phpmyadmin/" in your browser
address bar. You will see like below screenshot.
Creating our Database
Click Databases from the menu.
Type in the text box as pizza for the
database name and click "Create" button.
You will see your database name in the left menu like below screenshot.
Click your database name. You will see like below screenshot.
Type order in the
Name field and type 4 in the
Number of columns field like below screenshot. And then click Go button to crate a database table.
Now you need to fill the field name, type etc. like below screenshot.
A database table should have a
primary key. So for our order table id field should be the primary key. Select in the
Index select box as
PRIMARY and set the
A_I(AUTO_INCREMENT) check box that will increase automatically our id field number.
Then click
Save button.
Now you have created a database table to save your
customer order.
PHP and MySQL
It is time to
connect the MySQL database by using PHP.
Firstly I want to introduce you to the PHP built-in functions that can connect to the MySQL.
mysqli_connect("
hostname","username","password","databasename")
Above function will help you to connect the MySQL database. So you need to know the parameter values.
hostname - Hostname running database server. Default is localhsot. For our xampp or wamp is also localhost.
username - Our xampp or wamp default username is root.
password - Our xampp or wamp default password is blank(no password).
databasename - Our database name is pizza.
You need to add below code to our process.php.
<?php
$con = mysqli_connect('localhost','root','','pizza') or die(mysqli_error());
$sql="INSERT INTO `order` (`name`, `address`, `quantity`) VALUES ('$cus_name', '$address', '$quantity')";
mysqli_query($con,$sql) or die(mysqli_error());
?>
First line will connect to our MySQL sever and it will also return a object which represents the connection to a MySQL Server or
FALSE if the connection failed. We need to use this return object in other php function.
In second line, I create a variable of
SQL statement that will use in the query function. We can write this SQL statement in the query function directly but we do so for the simplicity. This SQL statement will insert the post data from user order to the order table of our pizza database.
The third line will run our SQL Query.
Our process.php file will be like below.
<?php
$title = "Pizza Shop: Order Process";
include('header.php');
include('sidebar.php');
?>
<div id="content">
<?php
if(isset($_POST['submit']))
{
if( $_POST['cus_name'] != '' && $_POST['quantity'] != '' && $_POST['address'] !='' )
{
$cus_name = $_POST['cus_name'];
$quantity = $_POST['quantity'];
$address = $_POST['address'];
$con = mysqli_connect('localhost','root','','pizza') or die(mysqli_error());
$sql="INSERT INTO `order` (`name`, `address`, `quantity`) VALUES ('$cus_name', '$address', '$quantity')";
mysqli_query($con,$sql) or die(mysqli_error());
?>
<p>Thank <?php echo $cus_name; ?> !</p>
<p>You order <?php echo $quantity; ?> pizza.</p>
<?php
$total = $quantity * 10;
?>
<p>It will cost you <?php echo $total; ?> $.</p>
<p>We will send withing 2 hours to <?php echo $address; ?>.</p>
<?php
}
else
{
$_SESSION['error'] = "You need to fill all data";
header("location: index.php");
}
?>
<?php } ?>
</div><!--end of content -->
<?php
include('footer.php');
?>
i did EXACTLY what it said to do