You are here: Re: PHP and MySQL issue « All PHP « IT news, forums, messages
Re: PHP and MySQL issue

Posted by Giannis Vrentzos on 08/03/06 13:12

mpar612@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!
>
> 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 <select> menu
> function input_select($element_name, $selected, $options, $multiple =
> false) {
>
> // print out the <select> tag
> 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 <option> tags
> 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);
> }
> }
> }
> ?>
>


Try to change the last foreach loop with the following. If this won't
work, try print_r($results) to see array's values.

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

Gvre

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация