Jump to content
Larry Ullman's Book Forums

Add_Print.Php From E-Commerce Chapter Issues


Recommended Posts

Hi,

 

I'm having some issues with a script based on the add_print.php in the e-commerce chapter of your PHP6 and MySQL 5 book. Basically, I can't get the form to insert into my database or retrieve from the database. It's not the mysql_connect.php file that's the issue as this works for other files. I'm not entirely convinced I have done the form correctly. It shows in the web browser fine but just doesn't update my database. Any help would be greatly appreciated. Below is my coding. Apologies for the long post.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Add Booking</title>

</head>

<?php

include ('./mainMenuUser.php');

?>

<body>

<?php # Script 13.1 - add_booking.php

//This page allows admin to add facility booking

 

require_once ('../mysql_connect.php');

 

if(isset($_POST['submitted'])) {

 

//Validate data

$errors = array();

 

//Check for customer first name

if(!empty($_POST['firstname'])) {

$fn = trim($_POST['firstname']);

} else{

$errors[] = 'Please enter the customer\'s First Name.';

}

 

//Check for customer last name

if(!empty($_POST['lastname'])) {

$ln = trim($_POST['lastname']);

} else{

$errors[] = 'Please enter the customer\'s Last Name.';

}

 

// Check for e-mail address

if (!empty($_POST['email'])) {

$e[] = trim($_POST['email']);

}else {

$errors[] = 'You forgot to enter the customer\'s e-mail address.';

}

 

// Check for telephone number

if (!empty($_POST['tel'])) {

$tn = trim($_POST['tel']);

}else {

$errors[] = 'You forgot to enter the customer\'s telephone number.';

}

 

// Check for function type

if (!empty($_POST['function_type'])) {

$ft = trim($_POST['function_type']);

}else {

$errors[] = 'You forgot to enter the function type.';

 

}

 

// Check for function room

if (!empty($_POST['function_room'])) {

$fr = trim($_POST['function_room']);

}else {

$errors[] = 'You forgot to enter the function room.';

}

 

// Check for date

if (!empty($_POST['date'])) {

$d = trim($_POST['date']);

}else {

$errors[] = 'You forgot to enter the date of the function.';

}

 

// Check for time

if (empty($_POST['time'])) {

$t = trim($_POST['time']);

}else {

$errors[] = 'You forgot to enter the time of the function.';

}

 

// Check for bar requirement

if (!empty($_POST['bar'])) {

$b = trim($_POST['bar']);

}else {

$errors[] = 'You forgot to enter whether a bar is required.';

}

 

// Check for bar extension requirement

if (!empty($_POST['extension'])) {

$ex = trim($_POST['extension']);

}else {

$errors[] = 'You forgot to enter whether a bar extension is required.';

}

 

// Check for catering requirement

if (!empty($_POST['catering'])) {

$c = trim($_POST['catering']);

}else {

$errors[] = 'You forgot to enter whether catering is required.';

}

 

// Check for DJ requirement

if (!empty($_POST['dj'])) {

$dj = trim($_POST['dj']);

}else {

$errors[] = 'You forgot to enter whether a DJ is required.';

}

 

//Validate booking

if(isset($_POST['Bookings']) && ($_POST['Bookings'] == 'new') ) {

//If new booking, add to database

 

//Check for firstname

if(!empty($_POST['firstname'])) {

$fn = trim($_POST['firstname']);

}

 

//Check for lastname

if(!empty($_POST['lastname'])) {

$ln = trim($_POST['lastname']);

}

 

//Check for e-mail

if(!empty($_POST['email'])) {

$e = trim($_POST['email']);

}

 

//Check for telephone number

if(!empty($_POST['tel'])) {

$tn = trim($_POST['tel']);

}

 

//Check for function type

if(!empty($_POST['function_type'])) {

$ft = trim($_POST['function_type']);

}

 

//Check for function room

if(!empty($_POST['function_room'])) {

$fr = trim($_POST['function_room']);

}

 

//Check for date

if(!empty($_POST['date'])) {

$d = trim($_POST['date']);

}

 

//Check for time

if(!empty($_POST['time'])) {

$t = trim($_POST['time']);

}

 

//Check for bar requirement

if(!empty($_POST['bar'])) {

$b = trim($_POST['bar']);

}

 

//Check for extension requirement

if(!empty($_POST['extension'])) {

$ex = trim($_POST['extension']);

}

 

//Check for catering requirement

if(!empty($_POST['catering'])) {

$c = trim($_POST['catering']);

}

 

//Check for DJ requirement

if(!empty($_POST['dj'])) {

$dj = trim($_POST['dj']);

}

 

//Add booking to database

$q = 'INSERT INTO Bookings (booking_id, firstname, lastname, email, tel, function_type, function_room, date, time, bar, extension, catering, dj) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';

$stmt = mysqli_prepare($db, $q);

mysqli_stmt_bind_param($stmt, 'sss', $fn, $ln, $e, $tn, $ft, $fr, $d, $t, $b, $ex, $c, $d);

mysqli_stmt_execute($stmt);

 

//Check results

if(mysqli_stmt_affected_rows($stmt) == 1) {

echo '<p>The booking has been added.</p>';

$a = mysqli_stmt_insert_id($stmt);

} else{

$errors[] = 'The booking could not be added to the database.';

}

 

//Close statement

mysqli_stmt_close($stmt);

 

}else {

$errors[] = 'Please enter the booking details.';

}

 

}elseif (isset($_POST['Bookings']) && ($_POST['Bookings'] == 'existing') && ($_POST['existing'] > 0) ) {

$a = (int) $_POST['existing'];

} else{

$errors[] = 'Please enter or select booking.';

}

 

//Validate customer

if(isset($_POST['customers']) && ($_POST['customers'] == 'new') ) {

//If new booking, add to database

 

//Check for firstname

if(!empty($_POST['firstname'])) {

$fn = trim($_POST['firstname']);

}

 

//Check for lastname

if(!empty($_POST['lastname'])) {

$ln = trim($_POST['lastname']);

}

 

//Check for e-mail

if(!empty($_POST['email'])) {

$e = trim($_POST['email']);

}

 

//Check for telephone number

if(!empty($_POST['tel'])) {

$tn = trim($_POST['tel']);

}

}

 

//Add customer to database

$q = 'INSERT INTO customers (cust_id, firstname, lastname, email, tel) VALUES (?, ?, ?, ?, ?)';

$stmt = mysqli_prepare($db, $q);

mysqli_stmt_bind_param($stmt, 'isdsss', $fn, $ln, $e, $tn);

mysqli_stmt_execute($stmt);

 

//Check results

//if(mysqli_stmt_affected_rows($stmt) == 1) {

 

//Print message

//echo '<p>The customer has been added.</p>';

 

//$_POST = array();

 

//} else{

// echo '<p style="font-weight: bold; color: #FFFFFF">Your entry could not be processed due to a system error.</p>';

//}

 

mysqli_stmt_close($stmt);

 

//Display form

?>

<h2>Add Booking</h2>

<form name="add_booking" action"add_booking.php" method="post"/>

<fieldset><legend>Please complete the form to add a booking to the database:</legend>

 

<div><b>Customer:</b>

<p><input type="radio" name="customers" value="existing" <?php if (isset($_POST['customers']) && ($_POST['customers'] == 'existing') ) echo ' checked="checked"'; ?>/> Existing =>

<select name="existing"><option>Select One</option>

<?php

$q = "SELECT COUNT(*) FROM customers ORDER BY lastname, firstname ASC";

$r = mysqli_query($db, $q);

if(mysqli_num_rows($r) > 0) {

while ($row = mysqli_fetch_array ($r, MYSQL_NUM)) {

echo "<option value=\"$row[0]\"";

//Check for stickyness

if(isset($_POST['existing']) && ($_POST['existing'] == $row[0]) ) echo ' selected="selected"';

echo ">$row[1]</option>\n";

}

} else{

echo '<option>Please add a new customer.</option>';

}

mysql_close($db);

?>

</select></p>

 

<p><input type="radio" name="artist" value="new"<?php if (isset($_POST['customers']) && ($_POST['customers'] == 'new')) echo ' checked="checked"'; ?>"/> New =>

First Name: <input type="text" name="firstname" size="10" maxlength="15" value="<?php if (isset($_POST['firstname'])) echo $_POST['firstname']; ?>" />

Last Name: <input type="text" name="lastname" size="10" maxlength="30" value="<?php if (isset($_POST['lastname'])) echo $_POST['lastname']; ?>" />

Telephone Number: <input type="text" name="tel" size="11" maxlength="11" value="<?php if (isset($_POST['tel'])) echo $_POST['tel']; ?>" />

E-mail Address: <input type="text" name="email" size="30" maxlength="60" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" />

</div>

 

<p><b>Function Type:</b> <input type="text" name="function_type" size="30" maxlength="50" value="<?php if (isset($_POST['function_type'])) echo $_POST['function_type']; ?>" /></p>

 

<p><b>Function Room:</b>

<select name="function_room" value="<?php echo $_POST['function_room']; ?>" />

<option value="Bradley Room">Bradley Room</option>

<option value="Back Bar">Back Bar</option>

<option value="Members Bar">Members Bar</option>

<option value="Pitch">Pitch</option>

</select></p>

 

<p><b>Time of function:</b>

<select name="time" value="<?php echo $_POST['time']; ?>" />

<option value="7pm til 12am">Evening</option>

<option value="9am til 1pm">Morning</option>

<option value="1pm til 6pm">Afternoon</option>

<option value="All Day">All Day</option>

</select></p>

 

<p><b>Bar Required?:</b><value="<?php echo $_POST['bar']; ?>" />

<select name="bar">

<option value="Yes">Yes</option>

<option value="No">No</option>

</select></p>

 

<p><b>Bar Extension Required?:</b>

<select name="extension" <value="<?php echo $_POST['extension']; ?>" />

<option value="Yes">Yes</option>

<option value="No">No</option>

</select></p>

 

<p><b>Catering Required?:</b>

<select name="catering" value="<?php echo $_POST['catering']; ?>" />

<option value="Yes">Yes</option>

<option value="No">No</option>

</select></p>

 

<p><b>DJ Required?:</b>

<select name="dj" value="<?php echo $_POST['dj']; ?>" />

<option value="Yes">Yes</option>

<option value="No">No</option>

</select></p>

 

<p><b>Date of function:</b><name="date" value="<?php echo $_POST['date']; ?>" />

<?php # Script 2.5 - calendar.php

function make_calendar_pulldowns($m = NULL, $d = NULL, $y = NULL) {

 

// Creates array for months

$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

 

// Creates months pull-down menu

echo '<select name="months">';

foreach ($months as $key => $value) {

echo "<option value=\"$key\"";

if ($key == $m) {

echo ' selected="selected"';

}

echo ">$value</option>\n";

}

echo '</select>';

 

// Create days pull-down menu

echo '<select name="days">';

for ($days = 1; $days <= 31; $days++) {

echo "<option value=\"$days\"";

if ($days == $d) {

echo ' selected="selected"';

}

echo ">$days</option>\n";

}

echo '</select>';

 

// Create years pull-down menu

echo '<select name="years">';

for ($years = 2011; $years <= 2020; $years++) {

echo "<option value=\"$years\"";

if ($years == $y) {

echo ' selected="selected"';

}

echo ">$years</option>\n";

}

echo '</select>';

}

 

// Get today's information and call function

$dates = getdate();

make_calendar_pulldowns ($dates['mon'], $dates['mday'], $dates['year']);

 

// Print current day and time

echo '<p>Today is ' . date ('l') . ' . The current time is ' . date ('g:i a') .

'.</p>';

 

?>

</fieldset>

 

<div align="center"><input type="submit" name="submit" value="Submit" /></div>

<input type="hidden" name="submitted" value="TRUE" />

 

</form>

 

</body>

</html>

Link to comment
Share on other sites

Thanks for the reply. I did as you said but it still doesn't work. The pull down menu which in my file should list all existing customers (in yours it lists all artists) is empty so it isn't retrieving from the database there. Also, the validations are not working at the moment as if I type nothing in the form and click on Submit, it doesn't bring up the error messages. When i do click on Submit, it briefly starts looking like it is going to load something up but then doesn't. And despite doing as you say, nothing is being inputted into the database.

Link to comment
Share on other sites

 Share

×
×
  • Create New...