Skip to content
Advertisement

php pdo how to pull down and set default values from the database

$select_stmt = $db->prepare('SELECT * FROM new_product WHERE `user_id` = :id');
$select_stmt->execute(array(':id'=>$_GET['id']));
$row = $select_stmt->fetch(PDO::FETCH_ASSOC);
<div class="form-group">
  <label for="exampleFormControlSelect1">尺寸</label>
  <select class="form-control"  name ="Size" id="Size">
    <option>30*60</option>
    <option>15*90</option>
    <option>15*17.3</option>
  </select>
</div>

I would like to ask how I can use the foreach to select the value brought out of the database to the default value. For example: my database value is 15 * 90, how to select option =15 * 90 I use PDO here

Advertisement

Answer

You need to create a loop to process all the results from the query and add the relevant parts of each row to the HTML

<?php
$select_stmt = $db->prepare('SELECT xx,desc FROM new_product WHERE `user_id` = :id');
$select_stmt->execute(array(':id'=>$_GET['id']));

?>  
<div class="form-group">
    <label for="exampleFormControlSelect1">尺寸</label>
    <select class="form-control"  name ="Size" id="Size">
<?php
while ( $row = $select_stmt->fetch(PDO::FETCH_ASSOC)) :
    // these are just example column names as you dont tell us 
    // what the columns you want to use are called

    echo "<option value='$row[xx]'>$row[desc]</option>";

endwhile;
?>
    </select>
</div>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement