Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Aaron Agostini
Aaron Agostini
6,285 Points

shirt.php results in Data could not be retrieved from the database.

I'm working from the project zip file from the php course, and I've gotten everything working except for getting details for a single shirt. With everything else working, what could be missing from this or other files that would lead to the data not being retrieved? Here's my shirt.php code (though I haven't made any modifications from the original project zip file):

'''

<?php

require_once("../inc/config.php");
require_once(ROOT_PATH . "inc/products.php");

// if an ID is specified in the query string, use it if (isset($_GET["id"])) { $product_id = intval($_GET["id"]); $product = get_product_single($product_id); }

// a $product will only be set and not false if an ID is specified in the query // string and it corresponds to a real product. If no product is // set, then redirect to the shirts listing page; otherwise, continue // on and display the Shirt Details page for that $product if (empty($product)) { header("Location: " . BASE_URL . "shirts/"); exit(); }

$section = "shirts"; $pageTitle = $product["name"]; include(ROOT_PATH . "inc/header.php"); ?>

    <div class="section page">

        <div class="wrapper">

            <div class="breadcrumb"><a href="<?php echo BASE_URL; ?>shirts/">Shirts</a> &gt; <?php echo $product["name"]; ?></div>

            <div class="shirt-picture">
                <span>
                    <img src="<?php echo BASE_URL . $product["img"]; ?>" alt="<?php echo $product["name"]; ?>">
                </span>
            </div>

            <div class="shirt-details">

                <h1><span class="price">$<?php echo $product["price"]; ?></span> <?php echo $product["name"]; ?></h1>

                <form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
                    <input type="hidden" name="cmd" value="_s-xclick">
                    <input type="hidden" name="hosted_button_id" value="<?php echo $product["paypal"]; ?>">
                    <input type="hidden" name="item_name" value="<?php echo $product["name"]; ?>">
                    <table>
                    <tr>
                        <th>
                            <input type="hidden" name="on0" value="Size">
                            <label for="os0">Size</label>
                        </th>
                        <td>
                            <select name="os0" id="os0">
                                <?php foreach($product["sizes"] as $size) { ?>
                                <option value="<?php echo $size; ?>"><?php echo $size; ?> </option>
                                <?php } ?>
                            </select>
                        </td>
                    </tr>
                    </table>
                    <input type="submit" value="Add to Cart" name="submit">
                </form>

                <p class="note-designer">* All shirts are designed by Mike the Frog.</p>

            </div>

        </div>

    </div>

<?php include(ROOT_PATH . "inc/footer.php"); ?> '''

Peter Hatzer
Peter Hatzer
20,837 Points

Hi Aaron,

In the img attribute and a few others, your using two sets of double quotes, this is causing your code to break. So the img attribute is closing off the code at the start img in the product array, thinking that the code between double quotes is your img src code and also your alt code:

<img src="<?php echo BASE_URL . $product["img"]; ?>" alt="<?php echo $product["name"]; ?>">

So instead use single quotes inside the product arrays like this:

<img src="<?php echo BASE_URL . $product['img']; ?>" alt="<?php echo $product['name']; ?>">

Hope this helps.

Peter

1 Answer

Aaron Agostini
Aaron Agostini
6,285 Points

Thanks Peter, I'll have to play with that. The code is directly from the zip file from the lesson, hopefully that can be corrected.

EDIT: I'm being prompted to select a best answer, however, Peter's actual answer above isn't available for me to pick. So please read that answer.