Thanksgiving-in-a-Box Order Entry/Update

Customer
Servings
$result
"; show_orders(); } function add_order($customer, $servings) { global $conn; if (orders_for_customer($customer) != 0) return "$customer has already placed an order!"; $stmt = $conn->prepare("insert into tgborder(time, customer, servings) values(now(), :cust, :serv)"); $stmt->bindParam(':cust', $customer); $stmt->bindParam(':serv', $servings); $result = $stmt->execute(); if (!$result) { var_dump($stmt->errorInfo()); die("error!"); } else return "Order placed!"; } function update_order($customer, $servings) { global $conn; if (orders_for_customer($customer) == 0) return "$customer has not placed an order!"; $stmt = $conn->prepare("update tgborder set servings=:serv where customer=:cust"); $stmt->bindParam(':cust', $customer); $stmt->bindParam(':serv', $servings); $result = $stmt->execute(); if (!$result) { var_dump($stmt->errorInfo()); die("error!"); } $count = $stmt->rowCount(); if ($count == 1) return "Order updated!"; else { return "Unexpected result: $count rows updated! Notify a manager!"; } } function orders_for_customer($customer) { global $conn; $stmt = $conn->prepare("select * from tgborder where customer=:cust"); $stmt->bindParam(':cust', $customer); $result = $stmt->execute(); if (!$result) { var_dump($stmt->errorInfo()); die("error!"); } return count($stmt->fetchAll()); } function show_orders() { global $conn; $rset = $conn->query("select * from tgborder order by customer"); $orders = $rset->fetchAll(PDO::FETCH_ASSOC); $count = count($orders); if ($count) echo "

$count Order(s):

"; else echo "

No orders! Make some cold calls for hot turkey!"; foreach ($orders as $order) { echo "{$order['customer']}: {$order['servings']} servings
\n"; } echo "

"; } function newconn() { if (gethostname() === "cgi-vm.cs.arizona.edu") { $dbname = "whm_cs337f13"; $user = "cs337f13"; $pw = "tednelson"; $host = "mysql.cs.arizona.edu"; } else { $dbname = "tgbox"; $user = "root"; $pw = ""; $host = "localhost"; } $dsn = "mysql:host=$host;dbname=$dbname"; // Data source name $conn = new PDO($dsn, $user, $pw); return $conn; }