seb213 Posted January 24, 2007 Share Posted January 24, 2007 someone please help me wrap my head around this.Heres what i have.http://snackerz.com/bulkei want to pass the product_id (primary key) through a url dynamicly via PHP,to a page that displays the product detail, so im using,[quote]<?php echo $row_Recordset2['products_id']; ?>[/quote]once the id has been passed to the detail page it looks like[quote]index.php?products_id=278[/quote]how then do i take that products_id, and relate it to the recordset i have on the detail page, so i can display the products information? ive been told to use . $_REQUEST['ProductID'] after my SQL Query, but my page errors out when i do. Please helphere is my PHP/SQL if that helps[quote]<?php require_once('../Connections/conn_Snack.php'); ?><?php$maxRows_Recordset1 = 12;$pageNum_Recordset1 = 0;if (isset($_GET['pageNum_Recordset1'])) { $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];}$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;mysql_select_db($database_conn_Snack, $conn_Snack);$query_Recordset1 = "SELECT * FROM products_to_categories, products_description, products WHERE products_to_categories.products_id = products_description.products_id AND products.products_id = products_to_categories.products_id AND products_to_categories.categories_id = ";$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);$Recordset1 = mysql_query($query_limit_Recordset1, $conn_Snack) or die(mysql_error());$row_Recordset1 = mysql_fetch_assoc($Recordset1);if (isset($_GET['totalRows_Recordset1'])) { $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];} else { $all_Recordset1 = mysql_query($query_Recordset1); $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);}$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;?>[/quote] Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted January 24, 2007 Share Posted January 24, 2007 instead of REQUEST use GET Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted January 24, 2007 Share Posted January 24, 2007 if i understand correctly, you'll want to use the $_GET method.http://w3schools.com/php/php_get.asp should get you goin. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 24, 2007 Share Posted January 24, 2007 It helps if you post the error.The rest of you - $_REQUEST holds the $_GET and $_POST. Quote Link to comment Share on other sites More sharing options...
seb213 Posted January 24, 2007 Author Share Posted January 24, 2007 ive tyed using $_GET , ive tyred reading the W3C artical, but thats why im here becuse i dont understand what im doingtell me if this is right....if i want to get the product_id from the url i passed, the my query should look like[quote]$query_Recordset2 = "SELECT * FROM products_to_categories, products_description, products WHERE products_to_categories.products_id = products_description.products_id AND products.products_id = products_to_categories.products_id AND products_to_categories.categories_id = " .$_REQUEST['ProductID'];[/quote]????when i do that, i get this error.[quote]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1[/quote] Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted January 24, 2007 Share Posted January 24, 2007 Try this:[code]$query_Recordset2 = "SELECT * FROM products_to_categories, products_description, products WHERE products_to_categories.products_id = products_description.products_id AND products.products_id = products_to_categories.products_id AND products_to_categories.categories_id = '{$_REQUEST['product_id']}'";[/code] Quote Link to comment Share on other sites More sharing options...
seb213 Posted January 24, 2007 Author Share Posted January 24, 2007 i still get this error[quote] MySQL Error#: 1064You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'product_id']}'' at line 1[/quote] Quote Link to comment Share on other sites More sharing options...
dgiberson Posted January 24, 2007 Share Posted January 24, 2007 Try this.....[code]$pid = $_REQUEST['products_id'];$query_Recordset2 = "SELECT * FROM products_to_categories ptc INNER JOIN products_description pd ON ptc.products_id = pd.products_id INNER JOIN products p ON p.products_id = ptc.products_id WHERE ptc.categories_id = $pid";[/code] Quote Link to comment Share on other sites More sharing options...
seb213 Posted January 24, 2007 Author Share Posted January 24, 2007 [quote]pid = $_REQUEST['products_id'];$query_Recordset2 = "SELECT * FROM products_to_categories ptc INNER JOIN products_description pd ON ptc.products_id = pd.products_id INNER JOIN products p ON p.products_id = ptc.products_id WHERE ptc.categories_id = $pid";[/quote]na, sorry that didnt work. :( Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted January 24, 2007 Share Posted January 24, 2007 take out INNER and just use JOIN see what happens Quote Link to comment Share on other sites More sharing options...
dgiberson Posted January 24, 2007 Share Posted January 24, 2007 does that query run in Query Browser or phpMyAdmin ???? I know the syntax of what I wrote is right, could be that the $_REQUEST['varname] is wrong...... Quote Link to comment Share on other sites More sharing options...
seb213 Posted January 24, 2007 Author Share Posted January 24, 2007 this is what i get if i run the query in sql[quote]ErrorSQL-query : $pid = $_REQUEST['products_id']MySQL said:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$pid = $_REQUEST['products_id']' at line 1[/quote] Quote Link to comment Share on other sites More sharing options...
seb213 Posted January 24, 2007 Author Share Posted January 24, 2007 this is my first time doing such a complex Query with php, so maybe ive done somthing wrong somewere else thats messing up what you guys are tying to do. what would be the best practice way to join three tables, and display the information with the $_GET. im using dreamweaver to write most of the code, so maybe someone knows a better way. agin here is my php/sql[quote]<?php require_once('../Connections/conn_Snack.php'); ?><?php$maxRows_Recordset1 = 12;$pageNum_Recordset1 = 0;if (isset($_GET['pageNum_Recordset1'])) { $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];}$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;mysql_select_db($database_conn_Snack, $conn_Snack);$query_Recordset1 = "SELECT * FROM products_to_categories, products_description, products WHERE products_to_categories.products_id = products_description.products_id AND products.products_id = products_to_categories.products_id AND products_to_categories.categories_id = 77";$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);$Recordset1 = mysql_query($query_limit_Recordset1, $conn_Snack) or die(mysql_error());$row_Recordset1 = mysql_fetch_assoc($Recordset1);if (isset($_GET['totalRows_Recordset1'])) { $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];} else { $all_Recordset1 = mysql_query($query_Recordset1); $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);}$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;mysql_select_db($database_conn_Snack, $conn_Snack);$query_Recordset2 = "SELECT * FROM products_description, products WHERE products_description.products_id = products.products_id AND products_description.products_id AND products.products_id =";$Recordset2 = mysql_query($query_Recordset2, $conn_Snack) or die(mysql_error());$row_Recordset2 = mysql_fetch_assoc($Recordset2);$totalRows_Recordset2 = mysql_num_rows($Recordset2);?>[/quote] Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted January 24, 2007 Share Posted January 24, 2007 do you have the URL for us? Quote Link to comment Share on other sites More sharing options...
seb213 Posted January 24, 2007 Author Share Posted January 24, 2007 the url for my site? i posted in the fist posthttp://snackerz.com/bulke Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted January 24, 2007 Share Posted January 24, 2007 It looks like you may be doing your table joins wrong, Here is a JOINTutorial, maybe it will help:http://www.tizag.com/mysqlTutorial/mysqljoins.php Quote Link to comment Share on other sites More sharing options...
craygo Posted January 24, 2007 Share Posted January 24, 2007 couple things you might want to consider.1. Keep all the relevant information right in the table. Image name, name of product, everything. Only thing you should have to send in the url is the product_id2. You only need to connect to the database once no need to do it again in your second querytry this[code]<?php$pid = $_REQUEST['products_id'];$query_Recordset2 = "SELECT * FROM products_description JOIN products ON products_description.products_id = products.products_id WHERE products.products_id ='$pid'";$Recordset2 = mysql_query($query_Recordset2, $conn_Snack) or die(mysql_error());$row_Recordset2 = mysql_fetch_assoc($Recordset2);$totalRows_Recordset2 = mysql_num_rows($Recordset2);?>[/code]Ray Quote Link to comment Share on other sites More sharing options...
seb213 Posted January 24, 2007 Author Share Posted January 24, 2007 My database was orriginaly set up by zen cart, thats why it structured the way it is. The reason i have two querys is because i have a template that diplays recent products on the right, and it has a Repeat region applyed to it. I cant use the same record set for the products i display on the left becuase the query already has a Repeat region applyed to it, so i cant control the amont of results i want diplayed (thats why i use a second query with no repeat region.) - i dont know if that made sence but anyway thats why...[quote]http://www.tizag.com/mysqlTutorial/mysqljoins.php[/quote]as for the joins tutuorial link it doesent work, do you have another Quote Link to comment Share on other sites More sharing options...
seb213 Posted January 24, 2007 Author Share Posted January 24, 2007 oh and when i tyed this:[color=red]<?php$pid = $_REQUEST['products_id'];$query_Recordset2 = "SELECT * FROM products_description JOIN products ON products_description.products_id = products.products_id WHERE products.products_id ='$pid'";$Recordset2 = mysql_query($query_Recordset2, $conn_Snack) or die(mysql_error());$row_Recordset2 = mysql_fetch_assoc($Recordset2);$totalRows_Recordset2 = mysql_num_rows($Recordset2);?>[/color]i got this:[color=red]Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /www/snackerz/html/bulke/products/index.php on line 6[/color] Quote Link to comment Share on other sites More sharing options...
dgiberson Posted January 24, 2007 Share Posted January 24, 2007 [quote author=seb213 link=topic=123868.msg512575#msg512575 date=1169668922]this is what i get if i run the query in sql[quote]ErrorSQL-query : $pid = $_REQUEST['products_id']MySQL said:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$pid = $_REQUEST['products_id']' at line 1[/quote][/quote]is that in phpMyAdmin??? take out $pid and change it to a valid product_id Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted January 24, 2007 Share Posted January 24, 2007 PHP myadmin won't take variables, or $_REQUEST,$_POST, or $_GET Quote Link to comment Share on other sites More sharing options...
craygo Posted January 24, 2007 Share Posted January 24, 2007 That usually means the sql statement is wrongput this in after the sql[code]$query_Recordset2 = "SELECT * FROM products_description JOIN products ON products_description.products_id = products.products_id WHERE products.products_id ='$pid'";echo $query_Recordset2."<br>";[/code]And post what the wuery isRay Quote Link to comment Share on other sites More sharing options...
dgiberson Posted January 24, 2007 Share Posted January 24, 2007 the lesson that can be learned from this thread is plain & simple..... build your query in Query Browser / phpMyAdmin, once it's working then copy it into PHP and make the appropriate variable alterations.... saves many many hours of frustration Quote Link to comment Share on other sites More sharing options...
craygo Posted January 24, 2007 Share Posted January 24, 2007 OK that helped solve his problem Quote Link to comment Share on other sites More sharing options...
seb213 Posted January 24, 2007 Author Share Posted January 24, 2007 [quote author=craygo link=topic=123868.msg512650#msg512650 date=1169672650]That usually means the sql statement is wrongput this in after the sql[code]$query_Recordset2 = "SELECT * FROM products_description JOIN products ON products_description.products_id = products.products_id WHERE products.products_id ='$pid'";echo $query_Recordset2."<br>";[/code]And post what the wuery isRay[/quote][quote author=The Little Guy link=topic=123868.msg512646#msg512646 date=1169672289]PHP myadmin won't take variables, or $_REQUEST,$_POST, or $_GET[/quote] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.