Ordering a Pizza -- Using automatically generated primary key (SERIAL)

Your phone:   (required)
Your Address:
What size pizza? Top it with:

The code for the php page to insert the order is:

$phone = $_GET['phone']; $address = $_GET['address']; $size = $_GET['size'];
if (!$phone) die ("We need your phone number.");
$conn=pg_connect ('dbname=jensen') or die ('Connect failed ');
$query1 = 'INSERT into pizza values ($1, $2, $3) returning orderno';
$query2 = 'INSERT into top values ($1)';
$res = pg_query_params($query1, array($phone, $address, $size))
 or die("pizza query failed".pg_last_error());
foreach ($_GET['topping'] as $top)
pg_query_params($query2, array($top))
 or die("topping query failed".pg_last_error());
// query 1 returned the new order number...
$orderno = pg_fetch_result($res,0,0);
setcookie("pizza", $orderno); // remember the order number
Thank you.
<a href=pizzasee.php>Check on your order</a>
Notice that to get the order number, and set a cookie, we return it in the first INSERT query. Now the client computer will remember the (latest) order.

The code to show the order could be as follows: (The actual php files are on Osiris)
$order = $_COOKIE['pizza']; 
settype($order, 'int'); // no Bobby Tables
if (!$order) die ("Your pizza is lost.");
$conn=pg_connect ('dbname=jensen') or die ('Connect failed ');
$res = pg_query("select * from pizza natural join top where orderno=$order");
$first = pg_fetch_assoc($res) or die ('no such order');
print $first['phone']."<br>\n";
print $first['address']."<br>\n";
$sizes = array('S'=>'SMALL', 'M'=>'MEDIUM','L'=>'LARGE');
$size=$sizes[$first['size']]; // expand size
print "You have ordered a $size pizza on<br>\n";
print date("d F Y h:i a", strtotime($first['at']))."<hr>\n"; // nice date

print $first['top']."<br>\n"; // now the toppings
while ($next = pg_fetch_assoc($res))
print $next['top']."<br>\n";

Go to SQL notes for this example