i am new in php and i don’t get it why i am receiving an error for undefined index in price. i actually want to get the value of the price when i am selecting a product from the select option and once i select, the price of that product will automatically shown, and once i clicked the submit, i want to see the price of that product in database, but it shows the id of my product once i clicked the submit, so meaning my product_id and price is the same.
HTML
<form method="post" enctype="multipart/form-data">
<div class="card-body card-block">
<div class="form-group">
<label for="product_name" class=" form-control-label">Product Name</label>
<select class="form-control" name="product_name" id="product_name" onchange="get_price('')"required>
<option>Select Product Name</option>
<?php
$res=mysqli_query($con,"select id,name from product order by name asc");
while($row=mysqli_fetch_assoc($res)){
if($row['id']==$product_name){
echo "<option value=".$row['id']." selected>".$row['name']."</option>";
}else{
echo "<option value=".$row['id'].">".$row['name']."</option>";
}
}
?>
</select>
</div>
<div class="form-group">
<label for="Price" class=" form-control-label">Price of the Product</label>
<select class="form-control" disabled name="price" id="price">
<option></option>
<?php
$res=mysqli_query($con,"select id,price from product order by name asc");
while($row=mysqli_fetch_assoc($res)){
if($row['id']==$product_name){
echo "<option value=".$row['id']." selected>".$row['price']."</option>";
}else{
echo "<option value=".$row['id'].">".$row['price']."</option>";
}
}
?>
</select>
</div>
<div class="form-group">
<label for="Quantity" class=" form-control-label">Quantity</label>
<input type="number" name="quantity" placeholder="Enter qty" class="form-control" required value="<?php echo $quantity?>">
</div>
<button id="payment-button" name="submit" type="submit" class="btn btn-lg btn-info btn-block">
<span id="payment-button-amount">Submit</span>
</button>
<div class="field_error"><?php echo $msg?></div>
</div>
</form>
PHP
$product_name = '';
$price ='';
$quantity='';
if(isset($_POST['submit'])){
$product_name=$_POST['product_name'];
$price=$_POST['price']; //error
$quantity=$_POST['quantity'];
$res=mysqli_query($con,"select * from pos where product_id='$product_name'");
$check=mysqli_num_rows($res);
if($check>0){
if(isset($_GET['id']) && $_GET['id']!=''){
$getData=mysqli_fetch_assoc($res);
if($id==$getData['id']){
}else{
$msg="Product is already in the form";
}
}else{
$msg="Product is already in the form";
}
}
if($msg==''){
if(isset($_GET['id']) && $_GET['id']!=''){
mysqli_query($con,"update pos set product_id='$product_name',qty = '$quantity' where id='$id'");
}else{
mysqli_query($con,"insert into pos(product_id,qty,price) values('$product_name','$quantity', '$price')");
}
header('location:pos.php');
die();
}
}