473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

PHP and MySQL issue

Hi everyone,

I'm not sure if this is asking too much or not. I am trying to get the
following PHP code to work on my website. It utilizes PHP 5, MySQL 4.1
and the PEAR DB module.

I am having issues retrieving data from the form. It won't retrieve
all of the data only a part of it. All of the code is below and I also
attached the code for the main file. I seem to understand everything
except for the last 40 or so lines of code, which is where the SQL
statements come into play.

If anyone has any helpful hints or tips, I would really appreciate it.
Or if anyone can make a recommendation for a book or website that might
help me figure this out, that would be great too. Thanks!

Here is the code for the database:
CREATE TABLE lounge (
isbn INT NOT NULL,
artist_name VARCHAR(255),
album_title VARCHAR(255),
release_date VARCHAR(30),
description VARCHAR(255),
price VARCHAR(255)
)

Here is the code for formhelpers.php (this is required for the section
of code):
<?php
//print a text box
function input_text($element_name, $values, $size, $max_length) {
print '<input type="text" name="' . $element_name .'" value="';
print htmlentities($values[$element_name]) . '" size="' . $size . '"
maxlength="' . $max_length . '">';
}

//print a submit button
function input_submit($element_name, $label) {
print '<input type="submit" name="' . $element_name .'" value="';
print htmlentities($label) .'"/>';
}

//print a textarea
function input_textarea($element_name, $values) {
print '<textarea name="' . $element_name .'">';
print htmlentities($values[$element_name]) . '</textarea>';
}

//print a radio button or checkbox
function input_radiocheck($type, $element_name, $values,
$element_value) {
print '<input type="' . $type . '" name="' . $element_name .'" value="'
.. $element_value . '" ';
if ($element_value == $values[$element_name]) {
print ' checked="checked"';
}
print '/>';
}

//print a <selectmenu
function input_select($element_name, $selected, $options, $multiple =
false) {

// print out the <selecttag
print '<select name="' . $element_name;

// if multiple choices are permitted, add the multiple attribute
// and add a [] to the end of the tag name
if ($multiple) { print '[]" multiple="multiple'; }
print '">';

// set up the list of things to be selected
$selected_options = array();
if ($multiple) {
foreach ($selected[$element_name] as $val) {
$selected_options[$val] = true;
}
} else {
$selected_options[ $selected[$element_name] ] = true;
}

// print out the <optiontags
foreach ($options as $option =$label) {
print '<option value="' . htmlentities($option) . '"';
if ($selected_options[$option]) {
print ' selected="selected"';
}

print '>' . htmlentities($label) . '</option>';

}

print '</select>';

}
?>

Here is the code for the main page:
<?php

// Load PEAR DB
require 'DB.php';
// Load the form helper functions.
require 'formhelpers.php';

// Connect to the database
$db = DB::connect('mysql://');
if (DB::isError($db)) { die ("Can't connect: " . $db->getMessage()); }

// Set up automatic error handling
$db->setErrorHandling(PEAR_ERROR_DIE);

// Set up fetch mode: rows as objects
$db->setFetchMode(DB_FETCHMODE_OBJECT);

// 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']) {

// 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
?>
<form method="POST" action="<?php print $_SERVER['PHP_SELF']; ?>">
<table>
<?php print $error_text ?>

<tr><td>ISBN:</td>
<td><?php input_text('isbn', $defaults, '', '') ?></td></tr>

<tr><td>Artist Name:</td>
<td><?php input_text('artist_name', $defaults, '', '') ?></td></tr>

<tr><td>Album Title:</td>
<td><?php input_text('album_title', $defaults, '', '') ?></td></tr>

<tr><td>Release Date:</td>
<td><?php input_text('release_date', $defaults, '', ''); ?>
</td></tr>

<tr><td>Description:</td>
<td><?php input_text('description', $defaults, '', ''); ?>
</td></tr>

<tr><td>Price:</td>
<td><?php input_text('price', $defaults, '', ''); ?>
</td></tr>

<tr><td colspan="2" align="center"><?php
input_submit('search','Search'); ?>
</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() {
// Access the global variable $db inside this function
global $db;

// build up the query
$sql = 'SELECT isbn, artist_name, album_title, release_date,
description, price FROM lounge WHERE
isbn = ?';

// if a dish name was submitted, add to the WHERE clause
// we use quoteSmart() and strtr() to prevent user-enter wildcards
from working

if (strlen(trim($_POST['artist_name']))) {
$artist_name = $db->quoteSmart($_POST['artist_name']);
$artist_name = strtr($artist_name, array('_' ='\_', '%' =>
'\%'));
$sql .= " AND artist_name LIKE $artist_name";
}

if (strlen(trim($_POST['album_title']))) {
$album_title = $db->quoteSmart($_POST['album_title']);
$album_title = strtr($artist_name, array('_' ='\_', '%' =>
'\%'));
$sql .= " AND album_title LIKE $album_title";
}

if (strlen(trim($_POST['release_date']))) {
$release_date = $db->quoteSmart($_POST['release_date']);
$release_date = strtr($release_date, array('_' ='\_', '%' =>
'\%'));
$sql .= " AND release_date LIKE $release_date";
}

if (strlen(trim($_POST['description']))) {
$description = $db->quoteSmart($_POST['description']);
$description = strtr($description, array('_' ='\_', '%' =>
'\%'));
$sql .= " AND description LIKE $description";
}

if (strlen(trim($_POST['price']))) {
$price = $db->quoteSmart($_POST['price']);
$price = strtr($price, array('_' ='\_', '%' ='\%'));
$sql .= " AND price LIKE $price";
}

// Send the query to the database program and get all the rows back
$results = $db->getAll($sql, array($_POST['isbn']));

if (count($results) == 0) {
print 'No results were found.';
} else {
print '<table>';
print '<tr><th>ISBN</th><th>Artist Name</th><th>Album
Title</th><th>Release
Date</th><th>Description</th><th>Price</th></tr>';
foreach ($results as $artist_name) {

printf('<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>',
htmlentities($results->isbn),
$artist_name->artist_name, $album_title->album_title,
$release_date->release_date, $results->description, $results->price);
}
}
}
?>

Aug 3 '06 #1
1 3353
mp*****@gmail.com wrote:
Hi everyone,

I'm not sure if this is asking too much or not. I am trying to get the
following PHP code to work on my website. It utilizes PHP 5, MySQL 4.1
and the PEAR DB module.

I am having issues retrieving data from the form. It won't retrieve
all of the data only a part of it. All of the code is below and I also
attached the code for the main file. I seem to understand everything
except for the last 40 or so lines of code, which is where the SQL
statements come into play.

If anyone has any helpful hints or tips, I would really appreciate it.
Or if anyone can make a recommendation for a book or website that might
help me figure this out, that would be great too. Thanks!
<code snipped>

What exactly are you getting? Is the SQL returning no results or
partial results? Are you getting any errors on your SQL?

Also, while I don't use PEAR::DB, I question what you're trying to do in
this statement and others like it:

$artist_name = strtr($artist_name, array('_' ='\_', '%' ='\%'));

I'm not sure why you're replacing the characters. Maybe it has
something to do with how PEAR::DB works.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 3 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: atlasyeo | last post by:
Hi, my first time posting on a newsgroup. anyway, let's cut to the chase. I'm trying to migrate mysql database form one server to another server. So I copied the data from /var/lib/mysql to the...
0
by: Ryan Schefke | last post by:
------=_NextPart_000_0077_01C34C8B.2B90C960 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit ..I just sent this out to the win32 distribution list but no one has...
0
by: bruce | last post by:
Hi... Update.... We have the following setup in our httpd.conf file. We've tried to give what's related to the issue. We're trying to set up a virtual host for a test project. The behavior...
0
by: Heikki Tuuri | last post by:
Hi! Many people have complained over years that Borland's dbExpress driver does not work with MySQL and transactions, because it disconnects from mysqld after each SQL statement. The postings...
0
by: Mark Adams | last post by:
I am a relative newbie to MySQL. I had a Postfix+Courier+MySQL mail server running for several months. It took me a week or so to get it up and running in September. Now, I did a clean upgrade to...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
4
by: Richard | last post by:
Hi All, I've been trying to build a Ruby-on-Rails plus MySQL application. I'm running Ruby 1.8.2, Rails 1.1.4 and MySQL 5.0.15-nt over WinXP-Pro/SP2. I run under an Administrative account. ...
12
by: mistral | last post by:
phpMyAdmin 2.6.2 problem: can no connects to mySQL database: each time shown error #1045 - Access denied for user 'username'@'192.168.1.2' (using password: YES) Is seems, this is most common...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
15
by: Cirene | last post by:
I'm trying to convince my client that MySQL server is not the way to go for his project. Here are some reasons why... - If you use the MySQL Connector you are bound to GPL (unless you want to pay...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.