php从数据库中选择并填充多个字段

时间:2017-03-02 09:21:15

标签: php mysql

我正在开展销售点项目。我想在另一个领域选择一个产品回应产品价格。我找不到这样做的方法。这是我的代码

<?php
    include('../connect.php');
    $result = $db->prepare("SELECT * FROM products");
    $result->bindParam(':userid', $res);
    $result->execute();
?>
<form>
    <select>
    <?php
        for ($i = 0; $row = $result->fetch(); $i++) {
    ?>
        <option value="<?php echo $row['product_id'];?>">
            <?php echo $row['product_code']; ?>
            - 
            <?php echo $row['gen_name'];  ?>
        </option>
    <?php } ?>
    </select>
    <input type="number" name="qty"  min="1" placeholder="Qty" autocomplete="off" style="width: 68px; height:30px; padding-top:6px; padding-     bottom: 4px; margin-right: 4px; font-size:15px;" / required>
    <input type="number" name="pc" max="25" placeholder="disc" autocomplete="off" style="width: 68px; height:30px; padding-top:6px; padding-bottom: 4px; margin-right: 4px; font-size:15px;" />
    <input type="number" name="price" max="25" value="price goes here "  style="width: 68px; height:30px; padding-top:6px; padding-bottom: 4px; margin-right: 4px; font-size:15px;" />
    <input type="hidden" name="date" value="<?php echo date('m/d/y'); ?>"/>
</form>

2 个答案:

答案 0 :(得分:0)

你可以使用javascript轻松完成,看一下下面的示例,这样你就可以在任何你想要juse使用id或任何对你有用的价格回显价格

$("#mySelect").change(function(){
var price = $(this).val();
var fruitName = $(this).find(":selected").text();

$("#fruitPrince").text('You choose '+fruitName+' and the price is: $'+price);

})
@import "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css";
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"> </script>
<div class="container-fluid">
  <select class="form-control" id="mySelect">
  <option selected disabled> Choose a Fruit </option>
  <option value="10"> Banana </option>
  <option value="5"> Apple </option>
  <option value="3"> Orange </option>
  </select>
  <div class="bg-success">
    <p id="fruitPrince"> </p>
  </div>
</div>

答案 1 :(得分:0)

使用了Reynnan victor的例子。它对我有用。这是代码

<?php
include('../connect.php');
$result = $db->prepare("SELECT * FROM products");
$result->execute();
for ($i = 0; $row = $result->fetch(); $i++) {
?>
<option value="<?php
echo $row['price'];
?>" ><?php
echo $row['gen_name'];
?>
<?php
echo $row['product_code'];
?></option>
<?php
}
?></select>
<input type="number" name="qty"  min="1" placeholder="Qty" required/>
<text id="price" contenteditable="true" name="price"> 
 <script>$("#mySelect").change(function(){
var price = $(this).val();
$("#price").text(price);
})</script></text>