Success page + data entry for a store page The purpose of this code is to log the inputs of a form (which is a menu for a shop) to the database to be kept as records as well as printed out as a receipt at the store.

I have been learning how to code from the internet and tried to put together my knowledge in a project. The code is working, but I would like to adopt elegance and good coding practices.

&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;?php
require 'databaseaccess.php';
?&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;?php
//check order present
if(!($_POST))
{
    echo "error, order was not received!";
    echo "&lt;br&gt;";
    echo "return to &lt;a href='orderformwscart.html'&gt; order page &lt;/a&gt;";

}
else{
    $total = 0;
    $soba_size = $_POST['soba_size'];
    switch($soba_size){
    case "small":
    $total = $total + 4;
    break;
    case "reg":
    $total = $total + 6;
    break;
    case "large":
    $total = $total + 8;
    break;
    }
    $soba_type = $_POST['soba_type'];
    $dbtoppings = '';
    if ($_POST['topping']){
        foreach($_POST['topping'] as $topping){
            switch($topping){
            case "duck":
            $total = $total + 1;
            $dbtoppings = $dbtoppings.",".$topping;
            break;
            case "egg":
            $total = $total + 2;
            $dbtoppings = $dbtoppings.",".$topping;
            break;
            case "chicken":
            $total = $total + 3;
            $dbtoppings = $dbtoppings.",".$topping;
            break;
            case "teriyaki chicken":
            $total = $total + 4;
            $dbtoppings = $dbtoppings.",".$topping;
            break;
            }
        }
    }
    if($_POST['requests']){
    $request = $_POST['requests'];
    }
    else{
    $request = '';
    }
//begin data entry
$data1 = array('total' =&gt; $total);
$STH = $DBH-&gt;prepare("INSERT INTO orderlist(total) values (:total)");
$STH-&gt;execute($data1);
$lastId = $DBH-&gt;lastInsertId();
//dataentry2
$data2 = array('orderid' =&gt; $lastId, 'soba_size' =&gt; $soba_size, 'soba_type' =&gt; $soba_type, 'topping' =&gt; $dbtoppings,'request' =&gt; $request);
$STH = $DBH-&gt;prepare("INSERT INTO orderdetails(orderID,soba_size,soba_type,topping,request) values (:orderid,:soba_size,:soba_type,:topping,:request)");
$STH-&gt;execute($data2);
}
?&gt;

&lt;div class="jumbotron"&gt;
        &lt;h2 class="text-center"&gt;Thank you for ordering. Your order has been printed.
        &lt;p id ="time"&gt;
        &lt;/p&gt; &lt;/h2&gt;
        &lt;h3 class="text-center"&gt;Order Number: &lt;?php echo $lastId; ?&gt;
        &lt;/br&gt;
        Amount:$ &lt;?php echo $total;  ?&gt;  &lt;/h3&gt;
        &lt;h4 class="text-center"&gt;&lt;small&gt;Please show this page at the cashier to confirm your order. Thank you!&lt;/small&gt;&lt;/h4&gt;   
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
First and most important thing is to indent your code. Indenting provides an instant visual structure which makes it easy to read. More often than not, bugs and quirks can be easily avoided simply by good code indentation. PHP is especially susceptible to this because its code is mixed directly with the HTML output. There is actually an issue in your posted code where should there be an error, your code will still try to display the order details at the bottom.

Next thing you want to consider is creating your code in blocks. On a high level this is basic MVC code structure where the logic and the output are separated. You want to apply this as often as possible to make your code easier to read and maintain.

Make sure to close your database connection after opening it. I typically declare my database object outside the included class file in order to make it clearer what the variable is. This also helps your IDE out for providing popup advice, and code completion. If you're not using an IDE yet, try NetBeans or CodeLobster for PHP development.

Here's my take on your code. I obviously couldn't test it without your database details but it should give you some ideas.

&lt;?php
class OrderDetails{
    private $Number;
    private $Amount;
    private $db;

    public function OrderDetails(){
        require 'databaseaccess.php';
        $this-&gt;db = $DBH;       //should probably declare $DBH outside the included file to make code easier to read, and to help the ide

        $this-&gt;ProcessOrder();

        //close database connection here
    }

    public function getNumber(){
        return $this-&gt;Number;
    }

    public function getAmount(){
        return $this-&gt;Amount;
    }

    private function ProcessOrder(){
        $total = 0;
        $soba_size = $_POST['soba_size'];
        switch($soba_size){
            case "small":
                $total = $total + 4;
                break;
            case "reg":
                $total = $total + 6;
                break;
            case "large":
                $total = $total + 8;
                break;
            default:break;      //always have a default even if you're confident it won't be needed
        }
        $soba_type = $_POST['soba_type'];
        $dbtoppings = '';
        if ($_POST['topping']){
            foreach($_POST['topping'] as $topping){
                switch($topping){
                    case "duck":
                        $total = $total + 1;
                        $dbtoppings = $dbtoppings.",".$topping;
                        break;
                    case "egg":
                        $total = $total + 2;
                        $dbtoppings = $dbtoppings.",".$topping;
                        break;
                    case "chicken":
                        $total = $total + 3;
                        $dbtoppings = $dbtoppings.",".$topping;
                        break;
                    case "teriyaki chicken":
                        $total = $total + 4;
                        $dbtoppings = $dbtoppings.",".$topping;
                        break;
                    default:break;      //always have a default even if you're confident it won't be needed
                }
            }
        }
        if($_POST['requests']){
            $request = $_POST['requests'];
        }
        else{
            $request = '';
        }

        //begin data entry
        $data1 = array('total' =&gt; $total);
        $STH = $this-&gt;db-&gt;prepare("INSERT INTO orderlist(total) values (:total)");
        $STH-&gt;execute($data1);
        $lastId = $this-&gt;db-&gt;lastInsertId();
        //dataentry2
        $data2 = array('orderid' =&gt; $lastId, 'soba_size' =&gt; $soba_size, 'soba_type' =&gt; $soba_type, 'topping' =&gt; $dbtoppings,'request' =&gt; $request);
        $STH = $this-&gt;db-&gt;prepare("INSERT INTO orderdetails(orderID,soba_size,soba_type,topping,request) values (:orderid,:soba_size,:soba_type,:topping,:request)");
        $STH-&gt;execute($data2);

        //set the class variables for grabbing by your other code
        $this-&gt;Amount = $total;
        $this-&gt;Number = $lastId;
    }
}
?&gt;&lt;!DOCTYPE&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;title&gt;Untitled Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;?php
    //check order present
    if(!($_POST))
    {
        ?&gt;
        error, order was not received!
        &lt;br/&gt;
        return to &lt;a href='orderformwscart.html'&gt; order page &lt;/a&gt;
        &lt;?php
    }
    else{
        $orderObj = new OrderDetails();
        ?&gt;
        &lt;div class="jumbotron"&gt;
            &lt;h2 class="text-center"&gt;
                Thank you for ordering. Your order has been printed.
                &lt;p id="time"&gt;&lt;/p&gt;
            &lt;/h2&gt;
            &lt;h3 class="text-center"&gt;
                Order Number: &lt;?php echo $orderObj-&gt;getNumber();?&gt;
                &lt;br/&gt;
                Amount: $ &lt;?php echo $orderObj-&gt;getAmount();?&gt;
            &lt;/h3&gt;
            &lt;h4 class="text-center"&gt;
                &lt;small&gt;Please show this page at the cashier to confirm your order. Thank you!&lt;/small&gt;
            &lt;/h4&gt;   
        &lt;/div&gt;
        &lt;?php
    }
    ?&gt;
&lt;/body&gt;
&lt;/html&gt;