Skip to content
Advertisement

passing values from selected option in form

<form method="post">
  <div class="form-group">
      <label >Product</label>
    <select  class="form-control" name="product" id="supplier-select" >

  <option value="">Select a Product </option>
                <?php
                $link = mysqli_connect();
    if (!$link) {
    die('Could not connect: ' . mysqli_connect_error());
}
                $query="select id,name from product";
                $result = mysqli_query($link,$query);
                    while($row = mysqli_fetch_array($result)){
  echo "<option value=".$row['id']."name='product' >".$row['name']."</option>";
}
?></select>
  </div>
                      <div class="form-group">
    <label >Date</label>
    <input type="date" name="date" class="form-control" id="" >
  </div>
  <div class="form-group">
    <label >Sale Quantity</label>
    <input type="text" name="quantity" class="form-control" id="">
  </div>
                      <div class="form-group">
    <label >Sale Price</label>
    <input type="text" name="price" class="form-control" id="">
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>
    
             <?php 
                  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      $id = $_POST['id'];
      $product = $row['product'];
      $date =$_POST['date'];
      $quan = $_POST['quantity'];
      $price = $_POST['price'];
    $link= mysqli_connect();
if(!$link){
    die ('connection unsuccessful'. mysqli_connect_error($link));
}
      $sql = "INSERT INTO items_sale (sale_id, prod_id, date, sale_quantity, sale_price) VALUES ('$id','$product','$date','$quan','$price')";
      if (mysqli_query($link, $sql)) {
          exit();
} else {
  echo "Error: " . $sql . "<br>" . mysqli_error($link);
}

how do I pass in value from selected option from to insert query in this code here. i have using row[] but the value is not passing on in the from to my insert query when I selected. in past i have tried POST[‘id’] but it select the same value as the id before.

Advertisement

Answer

Try changing line

echo "<option value=".$row['id']."name='product' >".$row['name']."</option>";

to

echo "<option value=".$row['id'].">".$row['name']."</option>"

$row[‘product’] doesn’t exist – try changing the beginning of your php code to

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  $id = $_POST['id'];
  $product = $_POST['product'];
  ...
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement