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 trialWilliam J. Terrell
17,403 PointsI'm not seeing how the URLs for the shirts are working... [SOLVED]
In the video, he enters the URL in the form of "localhost/shirts/108". I'm not seeing how that works, as I would assume that, given that format, the browser would be looking for a page called 108 in the shirts folder, correct?
My instance of the project works just fine, with exception to this detail. Whenever I click on a shirt from the listings page, I get a 404.
What's more, when I type in a "108" in the Search page, it also says there are no results.
[SOLVED]
I see what was going on now; there was no index.php in the shirts subfolder, and it was using a RewriteRule in the .htaccess file to set shirts.php to function as the landing page whenever you navigate there.
Because my files are in a deeper nested folder - htdocs/treehouse/PDO_tutorial - I just needed to update the RewriteRule in the .htaccess file to reflect that.
The rule that was written there was:
RewriteRule ^/shirts/$ /shirts/shirts.php
I just had to update it to:
RewriteRule ^shirts/$ /treehouse/PDO_tutorial/shirts/shirts.php
And now it works :)
Thanks!
3 Answers
Tod Novak
3,173 PointsIn this instance, I think the 108 is a directory, not a page name. An html page should have an extension on it like .htm or .html (or .php).
Kevin Korte
28,149 PointsThat URL is querying the database using a $_GET variable, and returning an array based on if a product has the SKU number from the URL. If it does, it's returning that information back.
William J. Terrell
17,403 PointsI'm wondering, then, if it's not by database.php or config.php files...
// Config.php
<?php
// these two constants are used to create root-relative web addresses
// and absolute server paths throughout all the code
define("BASE_URL","/treehouse/PDO_tutorial/");
define("ROOT_PATH",$_SERVER["DOCUMENT_ROOT"] . "/treehouse/PDO_tutorial/");
define("DB_HOST", "localhost");
define("DB_NAME", "shirts4mike");
define("DB_PORT", "3306"); // default 3306
define("DB_USER", "root");
define("DB_PASS", "");
// Database.php
<?php
try {
$db = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";port=" . DB_PORT . ", "DB_USER", "DB_PASS");
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8'");
} catch (Exception $e) {
echo "Could not connect to the database.";
exit;
}
Thanks!
William J. Terrell
17,403 PointsBy the way, what does the "MOD" mean accompanying your profile image? (I've seen others with "STAFF", but I've no idea what "MOD" means)
Thanks!
Kevin Korte
28,149 PointsIt's not either of those files. Where Randy is actually working in in the attached video is the products.php file in the include file. He's in the get_product_single
function, which is called in the shirt.php file. Basically it's this:
<?php
if (isset($_GET["id"])) {
$product_id = intval($_GET["id"]);
$product = get_product_single($product_id);
}
Which says if an ID is set (i.e. "108") than call get_product_single
and pass the function the ID, which should be the SKU, which is used to make a DB call to select name, price, img, sku, paypal from the products table where the sku the query from the string.
<?php
function get_product_single($sku) {
require(ROOT_PATH . "inc/database.php");
try {
$results = $db->prepare("SELECT name, price, img, sku, paypal FROM products WHERE sku = ?");
$results->bindParam(1,$sku);
$results->execute();
} catch (Exception $e) {
echo "Data could not be retrieved from the database.";
exit;
}
$product = $results->fetch(PDO::FETCH_ASSOC);
return $product;
}
The MOD just means I'm a moderator on the forum. I have the ability along with my fellow MODS to edit post, select best answers, change or add tags, etc..just basic stuff to help the forum run smoother. We're not paid by treehouse, we just volunteer and were picked by the staff at treehouse to mod based on our previous interactions with the forums.
The guys with the STAFF under their image actually work for Treehouse.
William J. Terrell
17,403 PointsThere must be something wrong with my database.php code anyway, because when I try to go back to the index page, I get this error:
Parse error: syntax error, unexpected 'DB_USER' (T_STRING) in C:\xampp\htdocs\treehouse\PDO_tutorial\inc\database.php on line 4
Kevin Korte
28,149 PointsIt sounds like your database user is wrong in your config file. Which could be, as you can set your database user and password to whatever you want, the config file just has to reflect that change.
William J. Terrell
17,403 PointsIt's the same as it was before, when it worked.
When I change it back to this code, in database.php, it works:
$db = new PDO("mysql:host=localhost;
dbname=shirts4mike;
port=3306;",
"root",
"");
But I still get the "Object not found!" when I try to put in this URL:
Michael Dowell
2,390 Points(Hmm... I just tried to post this answer previously and got a 500 error from teamtreehouse.com.. ok I will try again...)
I have a similar problem. This link: http://localhost:8888/shirts/108/
results in this error: 404 page not found
I have downloaded the new improved .htaccess file from the downloads page at this link: https://teamtreehouse.com/library/using-php-with-mysql/filtering-input-for-queries/refactoring-shirt-details
I copied the .htaccess contents to my htdocs folder in MAMP.
And the result was the error changed to: Server Error 500
Not sure how to make this work. Any suggestions?