473,566 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($ele ment_name, $values, $size, $max_length) {
print '<input type="text" name="' . $element_name .'" value="';
print htmlentities($v alues[$element_name]) . '" size="' . $size . '"
maxlength="' . $max_length . '">';
}

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

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

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

//print a <selectmenu
function input_select($e lement_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="multi ple'; }
print '">';

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

// print out the <optiontags
foreach ($options as $option =$label) {
print '<option value="' . htmlentities($o ption) . '"';
if ($selected_opti ons[$option]) {
print ' selected="selec ted"';
}

print '>' . htmlentities($l abel) . '</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.ph p';

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

// Set up automatic error handling
$db->setErrorHandli ng(PEAR_ERROR_D IE);

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

// 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($erro rs = '') {
// 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($erro rs)) {
$error_text = '<tr><td>You need to correct the following
errors:';
$error_text .= '</td><td><ul><li> ';
$error_text .= implode('</li><li>',$error s);
$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('isb n', $defaults, '', '') ?></td></tr>

<tr><td>Artis t Name:</td>
<td><?php input_text('art ist_name', $defaults, '', '') ?></td></tr>

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

<tr><td>Relea se Date:</td>
<td><?php input_text('rel ease_date', $defaults, '', ''); ?>
</td></tr>

<tr><td>Descrip tion:</td>
<td><?php input_text('des cription', $defaults, '', ''); ?>
</td></tr>

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

<tr><td colspan="2" align="center"> <?php
input_submit('s earch','Search' ); ?>
</td></tr>

</table>
<input type="hidden" name="_submit_c heck" value="1"/>
</form>
<?php
} // The end of show_form()

function validate_form() {
$errors = array();

// isbn is required
if (! strlen(trim($_P OST['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($_P OST['artist_name']);
$artist_name = strtr($artist_n ame, array('_' ='\_', '%' =>
'\%'));
$sql .= " AND artist_name LIKE $artist_name";
}

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

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

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

if (strlen(trim($_ POST['price']))) {
$price = $db->quoteSmart($_P OST['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>Descript ion</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($r esults->isbn),
$artist_name->artist_name, $album_title->album_title,
$release_date->release_date , $results->description, $results->price);
}
}
}
?>

Aug 3 '06 #1
1 3365
mp*****@gmail.c om 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_n ame, 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*******@attgl obal.net
=============== ===
Aug 3 '06 #2

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

Similar topics

7
5561
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 other one.. and use INSERT INTO from the old database to the new one..so the Top level 'mysql' database has all the correct users and password and...
0
3365
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 replied.can someone on this list please help? The issue should be trivial for experienced MySQL users, I'm just a novice, thanks!
0
2020
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 that we're seeing is that we can type: http://foo.com but the url that gets displayed is
0
3312
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 below suggests that this problem might now be fixed by Borland. Best regards, Heikki Tuuri
0
2821
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 Mandrake 9.2 and am reinstalling everything. This thing is kicking my ass and I can't seem to get past it. I could really use some help if...
0
3931
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 version of this document can be found at: http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download
4
5354
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. I tried building the first example from Agile Web Development with Rails, 1st ed. Somehow the DB got corrupted and I couldn't recover. Instead of...
12
2704
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 problem for mySOL and phpMyAdmin. Extremelly ugly and inconvenient program.
6
38475
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 through this without much trouble. Programming knowledge is not required. Index What is SQL? Why MySQL? Installing MySQL. Using the MySQL...
15
1277
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 $$$) - Doesn't work as good "out of the box" with Visual Studio 2008 Any other reasons why SQL Server is better than MySQL? I was thinking about...
0
7666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.