Hi everyone,
Thanks for your responses! My complete code is listed below.
If I try to print $row or $rows, nothing is returned. But if I put the piece of code from my previous post in another page it does print.
I tried declaring the $album_title and $artist_name as globals and that does not do anything.
Does anyone have any other thoughts. Thanks for all of your time!
<?php
// Load PEAR DB
require 'DB.php';
// Load the form helper functions
require 'formhelpers.php';
// Connect to the database
$db = DB::connect('');
if (DB::isError($db)) { die ("Can't connect: " . $db->getMessage()); }
// Set up automatic error handling
$db->setErrorHandling(PEAR_ERROR_DIE);
// The main page logic:
// - If the form is submitted, validate and then process or redisplay
// - If it's not submitted, display
if ($_POST['_submit_check']) {
// If validate_form() returns errors, pass them to show_form()
if ($form_errors = validate_form()) {
show_form($form_errors);
} else {
// The submitted data is valid, so process it
process_form();
}
} else {
// The form wasn't submitted, so display
show_form();
}
function show_form($errors = '') {
// If the form is submitted, get defaults from submitted parameters
if ($_POST['_submit_check']) {
$defaults = $_POST;
}
// If errors were passed in, put them in $error_text (with HTML markup)
if (is_array($errors)) {
$error_text = '<tr><td>You need to correct the following errors:';
$error_text .= '</td><td><ul><li>';
$error_text .= implode('</li><li>',$errors);
$error_text .= '</li></ul></td></tr>';
} else {
// No errors? Then $error_text is blank
$error_text = '';
}
// Jump out of PHP mode to make displaying all the HTML tags easier
?>
<html>
<head>
<title>Water Music Records</title>
</head>
<form method="POST" enctype="multipart/form-data" action="<?php print $_SERVER['PHP_SELF']; ?>">
<table>
<?php print $error_text ?>
<tr><td width="86">ISBN:</td>
<td width="18"><?php input_text('isbn', $defaults, '20', '15') ?></td></tr>
<tr><td>Artist Name:</td>
<td><?php input_text('artist_name', $defaults, '20', '30') ?></td></tr>
<tr><td>Album Title:</td>
<td><?php input_text('album_title', $defaults, '20', '75') ?></td></tr>
<tr><td><p>Release Date:<br>
<font size="1"> ( YYYY-MM-DD)</font> </p>
</td>
<td><?php input_text('release_date', $defaults, '20', '10') ?></td></tr>
<tr><td>Description:</td>
<td><?php input_textarea('description', $defaults); ?>
</td></tr>
<tr><td>Price:</td>
<td><?php input_text('price', $defaults, '20', '6'); ?>
</td></tr>
</td></tr>
<tr><td>Image:</td>
<td><input type="hidden" name="MAX_FILE_SIZE" value="524288">
<input type="file" name="upfile" />
</td></tr>
<tr><td>Also Available (Top):</td>
<td>
<?php
print "<select name=\"Top\">";
global $db;
$rows = $db->getAll('SELECT isbn, artist_name, album_title, date_format(release_date, \'%M %d, %Y\') as release_date, date_format(add_date, \'%M %d, %Y\') as add_date, description, price, image FROM lounge');
foreach ($rows as $row) {
$artist_name = stripslashes($row[artist_name]);
$album_title = stripslashes($row[album_title]);
print "<option value=\"$row[isbn]\">$artist_name - $album_title</option>";
}
print "</select>";
?>
</td></tr>
<tr><td>Also Available (Middle):</td></tr>
<tr><td>Also Available (Bottom):</td></tr>
<tr><td colspan="2" align="center"><?php input_submit('save','Add'); ?>
</td></tr>
</table>
<input type="hidden" name="_submit_check" value="1"/>
</form>
<?php
} // The end of show_form()
function validate_form() {
$errors = array();
// isbn is required
if (! strlen(trim($_POST['isbn']))) {
$errors[] = 'Please enter an isbn number.';
}
return $errors;
}
function process_form() {
$filetypes = array ('image/gif', 'image/jpg');
if (in_array($_FILES['upfile']['type'], $filetypes)){
$currentname = $_FILES['upfile']['name'];
$newname = $currentname.date("Ymdhis");
$uploaddir = "uploads/";
if (move_uploaded_file($_FILES['upfile']['tmp_name'], $uploaddir.'/'.$newname)) {
print("file upload was successful");
} else {
print("file upload failed");
}
} else {
print "Please use a .gif or .jpg file";
}
// Access the global variable $db inside this function
global $db;
$imagename = $uploaddir.$newname;
// Insert the new cd into the table
$db->query('INSERT INTO lounge (isbn, artist_name, album_title, release_date, description, price, image)
VALUES (?,?,?,?,?,?,?)',
array(htmlentities($_POST['isbn']), htmlentities($_POST['artist_name']), htmlentities($_POST['album_title']), htmlentities($_POST['release_date']), nl2br(htmlentities($_POST['description'])), htmlentities($_POST['price']), $imagename));
// Tell the user that we added a dish.
print 'Added ' . htmlentities($_POST['artist_name']) .
' to the database.';
}
?>