PHP and MySQL issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mpar612@gmail.com

    #1

    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);
    }
    }
    }
    ?>

  • Jerry Stuckle

    #2
    Re: PHP and MySQL issue

    mpar612@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.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    Working...