Ria
Ria

Reputation: 526

Populate a dropdown menu with values from a Database

I have a database with different products in it. Each product is available in different colours. What I want to achieve is in a dropdown menu select the colours in which the product is available and then load the page with the product of chosen colour.

I have got the following: The product is initially chosen as follows:

if (isset($_GET['product_id'])) {
 $query_RS_Product = ("SELECT * FROM products WHERE id = :pid AND hidden ='no'");
 $RS_Product = $conn->prepare($query_RS_Product) or die(errorinfo());
$RS_Product->bindValue(':pid', $_GET['product_id']);
$RS_Product->execute();
$row_RS_Product =$RS_Product->fetch();

Now I want to select the colours that are available for this product and create a dropdown menu

<form><select name="productColor" id="pColor" class="dropmenu" >

<?php  
    $productcolor = $row_RS_Product['Model'];
    $productcolor="SELECT * FROM products WHERE (Model = ':productcolor'  AND hidden ='no' ORDER BY Color";
    $resultp=$conn->prepare($productcolor);
    $resultp->bindValue(':productcolor', $productcolor );
    $resultp->execute();

    while ($pcolor = $resultp->fetch($productcolor)) {
        echo '<option value="Select Color">' . htmlspecialchars($pcolor['Color']) . '</option>';
    }
   ?>
</select>
</form>

However the menu will not populate Any help welcome

Upvotes: 0

Views: 60

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74232

Transcribed from (my) comments.

Two obvious errors are in here WHERE (Model = ':productcolor'

1) The bracket ( which should be removed; that's a parse error right there.

2) The quotes around the placeholder; those need to be removed.

Then $resultp->bindValue(':productcolor', $productcolor ); You can't bind a "query", you need to bind a value/array.

You need to rename your variable in $productcolor="SELECT ... to something else, and using the variable for the (GET) array.

Both PHP's error reporting and PDO error handling would have been of help here.

References:

Upvotes: 1

Related Questions