For my first attempt at programming, I am making a simple webapp with php and MySQL, to enter customer information and store information about orders.
So far I’ve made a form to enter customer information, a page to edit that information and another page to show the different customers, which is all working fine.
I’ve run into a stump when trying to implement the next stage however.
I have an editcustomers page with 3 columns.
- Column 1 shows a form containing customer information, allowing it to
be edited. - Column 2 allows ordering of products and showing how many products were ordered
- Column 3 shows the total paid so far, and the total owing.
The code for the page I have at present:
<html>
<?php
$id = $_GET['id'];
require_once('connect.php');
$sth = $dbh->query("SELECT * FROM customers where id = '$id'");
$sth->setFetchMode(PDO::FETCH_ASSOC);
?>
<div id="main">
<div id="left">
<form name="myForm" action ="process.php" method ="post" >
<?php while($row = $sth->fetch()){ ?>
<p><input type = "hidden" name ="id" value="<?php echo $row["id"] ?>"/>
<p><input type = "text" name ="firstName" size ="30" value=" <?php echo $row["firstName"]?>"/>
<p><input type = "text" name ="lastName" size ="30" value="<?php echo $row["lastName"]?>"/>
<p></p>
<input type="submit" value="Update" />
<?php }?>
</div>
<div id="mid">
<p>Amount owed<br />
<input type = "text" name ="moneyOwed" size ="30" /></p>
<input type="submit" value="Pay" />
<p>Number of aaaa<br />
<input type = "text" name ="numAaa" size ="30" /></p>
<p>Number of bbbb<br />
<input type = "text" name ="numBbb" size ="30" /></p>
<p>
<input type="submit" value="Update" />
</div>
<div id="right">
<b>Total Balance</b>
<p> Money owed: </p>
<p> aaa total: </p>
<p> bbb total: </p>
<p> Total: </p>
<input type = "text" name ="pay" size ="20" /></p>
<input type="submit" value="Make Payment" />
</div>
<?php
$dbh =null;
?>
</body>
</html>
I have the following database structure:
- A customers table, basic information like firstname, lastname, etc along with an id field.
- A products table, listing the different products, only 1 row containing the costs for each product.
- A purchases table, with an id field , fields like numProductA, numProductB, showing the quantity of each product ordered
The problem I’m having is that in my editcustomerspage, I have a database query to read in information from the customers table to fill the fields in the form in my first column and a separate query to update it if that if the function chosen.
Updating the second formshould be OK, as I could use a hidden field to differentiate forms, however I am unsure how to read the information in from the Customers table and from the Purchases table so that I can populate the fields in my second form..
Part of the problem is that the purchases table may be empty for a given customer, if that customer has not yet placed an order.
I was considering getting rid of the purchases table, and tacking the fields onto the users table which would solve my problem, although I think that is generally considered bad practice?
The products will be fixed in the application, and are not going to change. Even so, would it still be better to have column names cost and name and each products as a record?
What are some approaches I could take to solving this problem?
Even what you currently are doing is considered bad practice because of the columns numProductA, numProductB. What happens when you’ll have 1000 products and a user orders different amounts from all of them. This work only if the list of products is fixed (i.e. coded into the application) and requires a lot of coding.
You would probably want to create a purchase_item table that has fields like: id, purchase_id, product_id, num_product. It would be like a line on a standard bill: purchase_id identifies which bill the line belongs to; product_id would show the product that line is about and num_product would show the amount (multiplier) on that line.
In this manner you don’t need to change the code when new products are added to the database.