The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-General - Cant get a select list working with joined table. HELP
Discuss Cant get a select list working with joined table. HELP in the PHP Development forum on Dev Shed. Cant get a select list working with joined table. HELP PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 3rd, 2012, 10:57 AM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 67
Time spent in forums: 15 h 54 m 55 sec
Reputation Power: 1
|
|
|
PHP-General - Cant get a select list working with joined table. HELP
i am going round in circles. Maybe a fresh set of eyes can help
i have the following table joined
prod table
ID (prod)
name
CatID
Size Table
SizeID
Size
Stock Table
StockID
ID (prod)
SizeID
Stock
i am trying to populate a select list with sizes from a product
PHP Code:
$var3_Recordset1 = "-1";
if (isset($_GET['ProdID'])) {
$var3_Recordset1 = $_GET['ProdID'];
}
mysql_select_db($database_poochie, $poochie);
$query_Recordset1 = sprintf("SELECT * FROM poochieProd, poochieStock, poochieSizes WHERE poochieProd.ProdID = %s" AND poochieStock.sizeID = poochieSizes.SizeID, GetSQLValueString($var3_Recordset1, "text"));
$Recordset1 = mysql_query($query_Recordset1, $poochie) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1)
<select name="os0" class="text" id="selectSize">
<option value="Select Size">Select Size</option>
<?php
$query2 = sprintf("
SELECT DISTINCT
stock.stockID, size.Size
FROM
poochieProd AS prod
LEFT JOIN pochieStock AS stock ON prod.ID = stock.ID
LEFT JOIN poochieSizes AS size ON stock.SizeID = size.SizeID
WHERE
prod.ID = '%s' AND stock.Stock > 0
ORDER BY
size.SizeID ASC", GetSQLValueString($var3_Recordset1, "int"));
$results2 = mysql_query($query2);
while($row2 = mysql_fetch_array($results2)){
?>
<option value="<?php echo $row2['Size']; ?>"><?php echo $row2['Size']; ?></option>
<?php
}
?>
</select>
i dont know where i am going wrong,
thanks in advance for any help
|

October 3rd, 2012, 11:30 AM
|
|
|
|
You need to be more specific. Post some output or something that will help us understand the problem. What is wrong or missing? What error messages are you getting? Did you echo $query2 and then copy and paste that into a command line session?
P.S. You should consider changing to PDO rather than use the depreciated MySQL library.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
|

October 3rd, 2012, 11:39 AM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 67
Time spent in forums: 15 h 54 m 55 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by gw1500se You need to be more specific. Post some output or something that will help us understand the problem. What is wrong or missing? What error messages are you getting? Did you echo $query2 and then copy and paste that into a command line session?
P.S. You should consider changing to PDO rather than use the depreciated MySQL library. |
ok the Output i got from the select list was
<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/usr/users1/ort/public_html/ch/product-detail.php</b> on line <b>132</b>
and on line 132 the code is
while($row2 = mysql_fetch_array($results2)){
|

October 3rd, 2012, 11:44 AM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 67
Time spent in forums: 15 h 54 m 55 sec
Reputation Power: 1
|
|
|
the full out from the select list is
<select name="os0" class="text" id="selectSize">
<option value="Select Size">Select Size</option>
<br />
<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/usr/users1/ort/public_html/ch/product-detail.php</b> on line <b>132</b><br />
</select>
|

October 3rd, 2012, 12:06 PM
|
|
|
Try debugging the warning with this:
PHP Code:
$results2 = mysql_query($query2) or die ("Error in query: $query2. ".mysql_error());
|

October 3rd, 2012, 12:17 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 67
Time spent in forums: 15 h 54 m 55 sec
Reputation Power: 1
|
|
|
this is helping..it gave me an error regarding a spelling error
pochieStock
i corrected this to read poochieStock then it has given an error
Error in query: SELECT DISTINCT stock.stockID, size.Size FROM poochieProd AS prod LEFT JOIN poochieStock AS stock ON Prod.ID = stock.ID LEFT JOIN poochieSizes AS size ON stock.SizeID = size.SizeID WHERE Prod.ID = '-1' AND stock.stock > 0 ORDER BY size.SizeID ASC. Unknown table 'Prod' in where clause
but i am unsure what table 'Prod' the error is referring to?
|

October 3rd, 2012, 12:29 PM
|
|
|
|
You must not have a table named 'Prod' in your database (show tables). What is that particular column (Prod.ID) supposed to be?
|

October 3rd, 2012, 12:34 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 67
Time spent in forums: 15 h 54 m 55 sec
Reputation Power: 1
|
|
|
the tables that i have and the joins are
poochieCat
CatID
name
poochieStock
stockID
ProdID
sizeID
stock
poochieSizes
SizeID
Size
poochieProd
ProdID
Prodname
CatID
|

October 3rd, 2012, 12:49 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 67
Time spent in forums: 15 h 54 m 55 sec
Reputation Power: 1
|
|
|
the tables i included in the first post are not the correct ones from this post, i incorrectly added them so the ones on my previous ones are the correct one
|

October 3rd, 2012, 12:49 PM
|
|
|
|
I think you want to remove the dot (.).
|

October 3rd, 2012, 12:59 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 67
Time spent in forums: 15 h 54 m 55 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by gw1500se I think you want to remove the dot (.). |
i have changes the query to
<?php
$query2 = sprintf("
SELECT DISTINCT
stock.stockID, size.Size
FROM
poochieProd AS prod
LEFT JOIN poochieStock AS stock ON Prod.ID = stock.ID
LEFT JOIN poochieSizes AS size ON stock.SizeID = size.SizeID
WHERE
prodID = '%s' AND stock.stock > 0
ORDER BY
size.SizeID ASC", GetSQLValueString($var3_Recordset1, "int"));
$results2 = mysql_query($query2);
while($row2 = mysql_fetch_array($results2)){
?>
and am now getting the results
Error in query: SELECT DISTINCT stock.stockID, size.Size FROM poochieProd AS prod LEFT JOIN poochieStock AS stock ON Prod.ID = stock.ID LEFT JOIN poochieSizes AS size ON stock.SizeID = size.SizeID WHERE prodID = '-1' AND stock.stock > 0 ORDER BY size.SizeID ASC. Column 'prodID' in where clause is ambiguous
so i changed this to prod.prodID and it returned
Error in query: SELECT DISTINCT stock.stockID, size.Size FROM poochieProd AS prod LEFT JOIN poochieStock AS stock ON Prod.ID = stock.ID LEFT JOIN poochieSizes AS size ON stock.SizeID = size.SizeID WHERE prod.prodID = '-1' AND stock.stock > 0 ORDER BY size.SizeID ASC. Unknown table 'Prod' in on clause
|

October 3rd, 2012, 01:07 PM
|
|
|
|
Yes because table 'Prod' does not exist. What table do you want 'ProdID' to be taken from? It exists in more than 1.
|

October 3rd, 2012, 01:10 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 67
Time spent in forums: 15 h 54 m 55 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by gw1500se Yes because table 'Prod' does not exist. What table do you want 'ProdID' to be taken from? It exists in more than 1. |
i presume in need it to come from the poochieStock table.
|

October 3rd, 2012, 01:11 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 67
Time spent in forums: 15 h 54 m 55 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by gw1500se Yes because table 'Prod' does not exist. What table do you want 'ProdID' to be taken from? It exists in more than 1. |
i thought the poochieProd AS prod
was telling the next part to see poochieProd as prod so
WHERE prod.ProdID would see this?
|

October 3rd, 2012, 01:13 PM
|
|
|
|
Nope. Use poochieProd.ProdID
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|