Home » Php » javascript – My table name is tbl_order and no datas are fetching to my database

javascript – My table name is tbl_order and no datas are fetching to my database

Posted by: admin February 25, 2020 Leave a comment

Questions:

The below is my source code. I need to fetch datas to my database. My database name is invoicewapp and table name is tbl_order. my table columns are

order_id, 
order_no,
order_date,
order_receiver_name,
order_total_before_tax,
order_total_tax1,
order_total_tax2,
order_total_tax,
order_total_after_tax,
order_datetime

Code i have:

<?php
//index.php  
include('database_connection.php');
$statement = $connect->prepare("
SELECT * FROM tbl_order 
ORDER BY order_id DESC
");
$statement->execute();
$all_result = $statement->fetchAll();
$total_rows = $statement->rowCount();
if(isset($_POST["create_invoice"]))
{ 
$order_total_before_tax = 0;
$order_total_tax1 = 0;
$order_total_tax2 = 0;
$order_total_tax3 = 0;
$order_total_tax = 0;
$order_total_after_tax = 0;
$statement = $connect->prepare("
INSERT INTO tbl_order 
(order_no, order_date, order_receiver_name, order_receiver_address, order_total_before_tax, order_total_tax1, order_total_tax2, order_total_tax3, order_total_tax, order_total_after_tax, order_datetime)
VALUES (:order_no, :order_date, :order_receiver_name, :order_receiver_address, :order_total_before_tax, :order_total_tax1, :order_total_tax2, :order_total_tax3, :order_total_tax, :order_total_after_tax, :order_datetime)
");
$statement->execute(
array(
':order_no'               =>  trim($_POST["order_no"]),
':order_date'             =>  trim($_POST["order_date"]),
':order_receiver_name'          =>  trim($_POST["order_receiver_name"]),
':order_receiver_address'       =>  trim($_POST["order_receiver_address"]),
':order_total_before_tax'       =>  $order_total_before_tax,
':order_total_tax1'           =>  $order_total_tax1,
':order_total_tax2'           =>  $order_total_tax2,
':order_total_tax3'           =>  $order_total_tax3,
':order_total_tax'            =>  $order_total_tax,
':order_total_after_tax'        =>  $order_total_after_tax,
':order_datetime'           =>  date("Y-m-d")
)
);
$statement = $connect->query("SELECT LAST_INSERT_ID()");
$order_id = $statement->fetchColumn();
for($count=0; $count<$_POST["total_item"]; $count++)
{
$order_total_before_tax = $order_total_before_tax + floatval(trim($_POST["order_item_actual_amount"][$count]));
$order_total_tax1 = $order_total_tax1 + floatval(trim($_POST["order_item_tax1_amount"][$count]));
$order_total_tax2 = $order_total_tax2 + floatval(trim($_POST["order_item_tax2_amount"][$count]));
$order_total_tax3 = $order_total_tax3 + floatval(trim($_POST["order_item_tax3_amount"][$count]));
$order_total_after_tax = $order_total_after_tax + floatval(trim($_POST["order_item_final_amount"][$count]));
$statement = $connect->prepare("
INSERT INTO tbl_order_item 
(order_id, item_name, order_item_quantity, order_item_price, order_item_actual_amount, order_item_tax1_rate, order_item_tax1_amount, order_item_tax2_rate, order_item_tax2_amount, order_item_tax3_rate, order_item_tax3_amount, order_item_final_amount)
VALUES (:order_id, :item_name, :order_item_quantity, :order_item_price, :order_item_actual_amount, :order_item_tax1_rate, :order_item_tax1_amount, :order_item_tax2_rate, :order_item_tax2_amount, :order_item_tax3_rate, :order_item_tax3_amount, :order_item_final_amount)
");
$statement->execute(
array(
':order_id'               =>  $order_id,
':item_name'              =>  trim($_POST["item_name"][$count]),
':order_item_quantity'          =>  trim($_POST["order_item_quantity"][$count]),
':order_item_price'           =>  trim($_POST["order_item_price"][$count]),
':order_item_actual_amount'       =>  trim($_POST["order_item_actual_amount"][$count]),
':order_item_tax1_rate'         =>  trim($_POST["order_item_tax1_rate"][$count]),
':order_item_tax1_amount'       =>  trim($_POST["order_item_tax1_amount"][$count]),
':order_item_tax2_rate'         =>  trim($_POST["order_item_tax2_rate"][$count]),
':order_item_tax2_amount'       =>  trim($_POST["order_item_tax2_amount"][$count]),
':order_item_tax3_rate'         =>  trim($_POST["order_item_tax3_rate"][$count]),
':order_item_tax3_amount'       =>  trim($_POST["order_item_tax3_amount"][$count]),
':order_item_final_amount'        =>  trim($_POST["order_item_final_amount"][$count])
)
);
}
$order_total_tax = $order_total_tax1 + $order_total_tax2 + $order_total_tax3;
$statement = $connect->prepare("
UPDATE tbl_order 
SET order_total_before_tax = :order_total_before_tax, 
order_total_tax1 = :order_total_tax1, 
order_total_tax2 = :order_total_tax2, 
order_total_tax3 = :order_total_tax3, 
order_total_tax = :order_total_tax, 
order_total_after_tax = :order_total_after_tax 
WHERE order_id = :order_id 
");
$statement->execute(
array(
':order_total_before_tax'     =>  $order_total_before_tax,
':order_total_tax1'         =>  $order_total_tax1,
':order_total_tax2'         =>  $order_total_tax2,
':order_total_tax3'         =>  $order_total_tax3,
':order_total_tax'          =>  $order_total_tax,
':order_total_after_tax'      =>  $order_total_after_tax,
':order_id'             =>  $order_id
)
);
header("location:index.php");
}
if(isset($_POST["update_invoice"]))
{
$order_total_before_tax = 0;
$order_total_tax1 = 0;
$order_total_tax2 = 0;
$order_total_tax3 = 0;
$order_total_tax = 0;
$order_total_after_tax = 0;
$order_id = $_POST["order_id"];
$statement = $connect->prepare("
DELETE FROM tbl_order_item WHERE order_id = :order_id
");
$statement->execute(
array(
':order_id'       =>      $order_id
)
);
for($count=0; $count<$_POST["total_item"]; $count++)
{
$order_total_before_tax = $order_total_before_tax + floatval(trim($_POST["order_item_actual_amount"][$count]));
$order_total_tax1 = $order_total_tax1 + floatval(trim($_POST["order_item_tax1_amount"][$count]));
$order_total_tax2 = $order_total_tax2 + floatval(trim($_POST["order_item_tax2_amount"][$count]));
$order_total_tax3 = $order_total_tax3 + floatval(trim($_POST["order_item_tax3_amount"][$count]));
$order_total_after_tax = $order_total_after_tax + floatval(trim($_POST["order_item_final_amount"][$count]));
$statement = $connect->prepare("
INSERT INTO tbl_order_item 
(order_id, item_name, order_item_quantity, order_item_price, order_item_actual_amount, order_item_tax1_rate, order_item_tax1_amount, order_item_tax2_rate, order_item_tax2_amount, order_item_tax3_rate, order_item_tax3_amount, order_item_final_amount) 
VALUES (:order_id, :item_name, :order_item_quantity, :order_item_price, :order_item_actual_amount, :order_item_tax1_rate, :order_item_tax1_amount, :order_item_tax2_rate, :order_item_tax2_amount, :order_item_tax3_rate, :order_item_tax3_amount, :order_item_final_amount)
");
$statement->execute(
array(
':order_id'                 =>  $order_id,
':item_name'                =>  trim($_POST["item_name"][$count]),
':order_item_quantity'          =>  trim($_POST["order_item_quantity"][$count]),
':order_item_price'            =>  trim($_POST["order_item_price"][$count]),
':order_item_actual_amount'     =>  trim($_POST["order_item_actual_amount"][$count]),
':order_item_tax1_rate'         =>  trim($_POST["order_item_tax1_rate"][$count]),
':order_item_tax1_amount'       =>  trim($_POST["order_item_tax1_amount"][$count]),
':order_item_tax2_rate'         =>  trim($_POST["order_item_tax2_rate"][$count]),
':order_item_tax2_amount'       =>  trim($_POST["order_item_tax2_amount"][$count]),
':order_item_tax3_rate'         =>  trim($_POST["order_item_tax3_rate"][$count]),
':order_item_tax3_amount'       =>  trim($_POST["order_item_tax3_amount"][$count]),
':order_item_final_amount'      =>  trim($_POST["order_item_final_amount"][$count])
)
);
$result = $statement->fetchAll();
}
$order_total_tax = $order_total_tax1 + $order_total_tax2 + $order_total_tax3;
$statement = $connect->prepare("
UPDATE tbl_order 
SET order_no = :order_no, 
order_date = :order_date, 
order_receiver_name = :order_receiver_name, 
order_receiver_address = :order_receiver_address, 
order_total_before_tax = :order_total_before_tax, 
order_total_tax1 = :order_total_tax1, 
order_total_tax2 = :order_total_tax2, 
order_total_tax3 = :order_total_tax3, 
order_total_tax = :order_total_tax, 
order_total_after_tax = :order_total_after_tax 
WHERE order_id = :order_id 
");
$statement->execute(
array(
':order_no'               =>  trim($_POST["order_no"]),
':order_date'             =>  trim($_POST["order_date"]),
':order_receiver_name'        =>  trim($_POST["order_receiver_name"]),
':order_receiver_address'     =>  trim($_POST["order_receiver_address"]),
':order_total_before_tax'     =>  $order_total_before_tax,
':order_total_tax1'          =>  $order_total_tax1,
':order_total_tax2'          =>  $order_total_tax2,
':order_total_tax3'          =>  $order_total_tax3,
':order_total_tax'           =>  $order_total_tax,
':order_total_after_tax'      =>  $order_total_after_tax,
':order_id'               =>  $order_id
)
);
$result = $statement->fetchAll();
header("location:index.php");
}
if(isset($_GET["delete"]) && isset($_GET["id"]))
{
$statement = $connect->prepare("DELETE FROM tbl_order WHERE order_id = :id");
$statement->execute(
array(
':id'       =>      $_GET["id"]
)
);
$statement = $connect->prepare(
"DELETE FROM tbl_order_item WHERE order_id = :id");
$statement->execute(
array(
':id'       =>      $_GET["id"]
)
);
header("location:index.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="noindex, nofollow">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<link href="css/style.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<script src="js/jquery.dataTables.min.js"></script>
<script src="js/dataTables.bootstrap.min.js"></script>
<link rel="stylesheet" href="css/dataTables.bootstrap.min.css">
</head>
How to&Answers: