Jump to content

Help with SQL Query and PHP (passing variables via URL)


seb213

Recommended Posts

someone please help me wrap my head around this.

Heres what i have.

http://snackerz.com/bulke

i 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 help

here 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]
Link to comment
Share on other sites

ive tyed using $_GET , ive tyred reading the W3C artical, but thats why im here becuse i dont understand what im doing

tell 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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

[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.  :(

Link to comment
Share on other sites

this is what i get if i run the query in sql

[quote]Error

SQL-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]

Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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_id

2. You only need to connect to the database once no need to do it again in your second query
try 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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

[quote author=seb213 link=topic=123868.msg512575#msg512575 date=1169668922]
this is what i get if i run the query in sql

[quote]Error

SQL-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
Link to comment
Share on other sites

That usually means the sql statement is wrong

put 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 is

Ray

Link to comment
Share on other sites

[quote author=craygo link=topic=123868.msg512650#msg512650 date=1169672650]
That usually means the sql statement is wrong

put 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 is

Ray


[/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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.