Query two tables on same page depending on ID
Moderator: General Moderators
Query two tables on same page depending on ID
Hey. I'm pretty new to php and am having a little trouble. I have one MySql table with 10 products that populates a HTML template based on an ID for each prouct. I also have 10 other tables that have ordering information for each product. What I am trying to do is bring up a product with a url such as ?id=3, and then bring up the ordering information of that page taken from the corresponding table. Here is the code that I am using, and following is the error that I am recieving. I can't seem to figure this out. Any help is greatly appreciated!
<?php
$product_array = array(
1 => "as",
2 => "ep",
3 => "es",
4 => "lc",
5 => "pen",
6 => "sb",
7 => "ssa",
8 => "ssamw",
9 => "ssb",
10 => "ssc",
//Some code here that gets the $product_id
$query = "SELECT * FROM products WHERE id = " . $_GET['id'] . ";";
//Then make the query
$query = "SELECT * FROM " . $product_array[$product_id] . " WHERE product_id = '$product_id'";
$result = mysql_query($query) or die(mysql_error());
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top">
<?php
while($row = mysql_fetch_object($result))
{
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="340" height="30"><?php echo($row->title); ?></td>
</tr>
<tr>
<td width="340"><?php echo($row->description); ?><p></p></td>
</tr>
<tr>
<td height="20">
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 'WHERE product_id = ''' at line 1[/i]
<?php
$product_array = array(
1 => "as",
2 => "ep",
3 => "es",
4 => "lc",
5 => "pen",
6 => "sb",
7 => "ssa",
8 => "ssamw",
9 => "ssb",
10 => "ssc",
//Some code here that gets the $product_id
$query = "SELECT * FROM products WHERE id = " . $_GET['id'] . ";";
//Then make the query
$query = "SELECT * FROM " . $product_array[$product_id] . " WHERE product_id = '$product_id'";
$result = mysql_query($query) or die(mysql_error());
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top">
<?php
while($row = mysql_fetch_object($result))
{
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="340" height="30"><?php echo($row->title); ?></td>
</tr>
<tr>
<td width="340"><?php echo($row->description); ?><p></p></td>
</tr>
<tr>
<td height="20">
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 'WHERE product_id = ''' at line 1[/i]
Looks as if the $product_id variable isn't being parsed in your SQL statement. Check to see if $product_id actually contains a value. If it does, you could also try changing this line:
to
Code: Select all
$query = "SELECT * FROM " . $product_array[$product_id] . " WHERE product_id = '$product_id'";Code: Select all
$query = "SELECT * FROM " . $product_array[$product_id] . " WHERE product_id = '".$product_id."'";Try replacing this bit
with this
If $product_id is set, the query will work, otherwise, it will print an error message.
Mark
Code: Select all
$query = "SELECT * FROM " . $product_array[$product_id] . " WHERE product_id = '$product_id'";
$result = mysql_query($query) or die(mysql_error());Code: Select all
if (isset($product_id)) {
$query = "SELECT * FROM " . $product_array[$product_id] . " WHERE product_id = '$product_id'";
$result = mysql_query($query) or die(mysql_error());
} else {
print "Product ID is not set!!!!";
}Mark
put this as your first line
THe variables you pass through the query string won't be automatically available in your script if your register globals setting is off, the above should solve the problem.
Is the code in your first post the entire script? if so, there are a number of errors in it that i have noticed.
If it isn't, could you post the entire code.
Mark
Code: Select all
extract($_GET);
$product_id = $id;Is the code in your first post the entire script? if so, there are a number of errors in it that i have noticed.
If it isn't, could you post the entire code.
Mark
The error is because you are selecting from a table called "as", but "as" is a reserved MySQL keyword!
i've re-written a bit of your code too
i've re-written a bit of your code too
Code: Select all
<?php
extract($_GET);
$product_id = $id;
$product_array = array(
1 => "as",
2 => "ep",
3 => "es",
4 => "lc",
5 => "pen",
6 => "sb",
7 => "ssa",
8 => "ssamw",
9 => "ssb",
10 => "ssc",);
//Some code here that gets the $product_id
$query = "SELECT * FROM products WHERE id = '" . $_GET['id'] . "'"; // Why is this line here? it will never get used!?
//Then make the query
$query = "SELECT * FROM " . $product_array[$product_id] . " WHERE product_id = '$product_id'";
$result = mysql_query($query) or die(mysql_error());
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
while($row = mysql_fetch_object($result))
{
?>
<tr>
<td width="340" height="30"><?php echo($row->title); ?></td>
</tr>
<tr>
<td width="340"><?php echo($row->description); ?><p></p></td>
</tr>
<tr>
<td height="20">
<tr>
<?php
}
?>