Hi everyone,

I am having a strange issue with the code posted below:

If I put this code into a page by itself with the db login info, it works just fine. But if I put it into another file that contains other PHP code, the select menu shows up with rows of "-" and not the values contained in the db.

I didn't want to make a long post, so here is a snippet of code. Is there anything I should be looking out for? Should I post all of my code (it's 159 lines long).

Any thoughts would be greatly appreciated. Thanks!

<?php
	print "<select name=\"Top\">";
	$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>";

?>

    Shot in the dark, but you didn't put your array keys in quotes here:

    $artist_name = stripslashes($row[artist_name]);
    $album_title = stripslashes($row[album_title]); 

    should be...

    $artist_name = stripslashes($row['artist_name']);
    $album_title = stripslashes($row['album_title']);

    I could be wrong, but clearly the artist_name and album_title variables aren't getting assigned so all that is printing is the '-' between them.

      Thanks, I tried that and it did not do anything. I get the same thing with the select menu with lines of "-"'s. Any other thoughts?

      Thanks for your help!

        What is the result if you were to print the $row variable. $rows probably doesn't contain data which means there is something wrong with your query or in the getAll function for the $db object.

          When you put this in to another php code, have you made sure that your $db, $artist_name, etc are actually the global variables that are being accessed/modified. You may have to explicitly declare them as global.

          Since you haven't posted all your code, this is kind of a guess.

            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.';
            }
            
            ?>

              I can't see why it doesn't work, but try a print_r( $rows ) to see what is being returned.
              It could be a matter of not returning an assoc array.

                Hi there,

                Thank you for the response. This is odd. print_r ( $rows ) returns absolutely nothing. I don't get it.

                Anyone have any other thoughts.

                Thanks!

                  I assume you put the print_r after the gettAll().
                  Try var_dump( $db ) to see if the DB object is created.

                    I did put the print_r after getAll. I did the var_dump and it did not return anything.

                    Thanks!

                      I would remove the global $db and put some more var_dump( $db ) in earlier on to see what is happening.

                        Write a Reply...