Unable to post value from input into database table

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

An input type=“button” doesn’t submit a form. What is your code for the onclick=“relocate_invoice();” function?

Next, a laundry list of problems -

  1. The global keyword only has meaning inside a function, where you want to reference a variable defined outside the function, and even in this case it should not be used. Your main application code should be responsible for creating the database connection and should pass it into any function that needs it as a call-time parameter.
  2. To submit an array of values from the form, the quantity field needs to be an array name, i.e. name=‘quantity[…]’ and the array index … needs to be the id of the item the quantity will correspond to. You would then use a foreach(){} loop to loop over the submitted data to get each id/quantity pair.
  3. Since the submitted data can contain zero values, you will either need to add conditional logic to only use non-zero quantities, or you can just use array_filter() on the submitted data to remove the zero quantities.
  4. You would prepare the INSERT query once, before the start of any looping and if you supply an array of the input data to the ->execute([…]) method call, you can eliminate the ->bindValue() statement. You would only be calling the ->execute([…]) statement inside of the loop.
  5. You would be inserting the item ids and the quantities into a table holding information about the items in an order. You would also need an order id to identify all the items in a single order. Your program flow should be - insert the unique/one-time information about the order into an order table. This will create an order id (auto-increment column) value. You would get the last inserted id value from this table and use it when inserting the item ids and quantities into an order_items table.

Hello @phdr,
Yes, the function code is this for the relocate_invoice():

<script>
      function relocate_invoice(){
            location.href = "invoice.php";
        }
 </script>

The only thing that function does is load a new page when you click the button. It doesn’t have anything to do with submitting the form.

I suggest you use a type=‘submit’ input field.

Sponsor our Newsletter | Privacy Policy | Terms of Service