I am trying to create a simple invoice system and trying to post the value from the input which is basically for quantity into the table named orders. I have this form here in which I have input where I have the step type. I have wrapped up my code into a form which is displaying table format. In this table format I have displayed on the page: model,description of product, price, and the quantity which is the step input. I made a connection to the database and tried to use post method but no value getting inserted into the database. Any help will be huge. Thanks in advance.
Here is the code for the items.php file where I am displaying the products and have quantity input:
<body>
<h1>Welcome to shopping</h1>
<div class="row">
<form method="post" class="form-horizontal col-md-6 col-md-offset-3" action="items.php">
<div class="form-group">
<table id="myTable">
<?php
global $pdo;
include 'database/database.php';
$pdo = db_connect();
$sql = "SELECT model, description,price,pic,quantity FROM items;";
$result = $pdo->query($sql);
echo '<tr>
<th>Model</th>
<th>Description</th>
<th>Price</th>
<th>Picture</th>
<th>Quantity</th>;
</tr>';
while($rows = $result->fetch()) {
echo '<tr> <td>'.$rows['model'].'</td>
<td>'.$rows['description'].'</td>
<td>'.$rows['price'].'</td>
<td>'. '<img src= "'.$rows['pic'].'" height="100" width="100">'.'</td>
<td>'.'<input type="number" placeholder="0" name="quantity" step="1" min="0" max="20">'.'</td>
</tr>';
}
?>
</table>
<input type="button" class="btn btn-primary col-md-2 col-md-offset-10" onclick="relocate_invoice();" value="Checkout"/>
</div>
</form>
</div>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
get_orders();
}
?>
</body>
</html>
And the database.php file:
<?php
require_once('database/config.php');
function db_connect(){
try {
$servername = DBHOST;
$databasename = DBNAME;
$user = DBUSER;
$password = DBPASS;
$pdo = new PDO("mysql:host=$servername;dbname=$databasename",$user,$password);
$pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
}
catch (PDOException $e)
{
die($e->getMessage());
}
}
function get_orders(){
global $pdo;
if($_SERVER["REQUEST_METHOD"] == "POST"){
$quantity = $_POST['quantity'];
$statement = $pdo->prepare("INSERT INTO orders (quantity) VALUES(:quantity);");
$statement->bindValue(':quantity',$quantity);
$statement->execute();
}
}
Below is the picture attached of the page for the items.php file