Dropdown fetching database table by ID

I have radio buttons on my form but when i clicked on radio it shows specific dropdown which fetch data from database table but i need to show table by specific foreignkey Id.

Table name--ABC
id| fruitid   | name
1 |  1        | Apple
2 |  1        | Mango
3 |  2        | Veg-1
4 |  3        | Juice-1
$fruitid= 'fruitid';////--> Can i use id here 
$getdata = mysqli_query($conn, "SELECT * FROM `ABC` WHERE `fruitid`= '$fruitid');
if(mysqli_num_rows($getdata) > 0){
                         while($data= mysqli_fetch_assoc($getdata)){
                          $tid   = $data['fruitid'];
                           $name = $data[' name'];
                    ?>      
                <option value="<?php echo $tid; ?>"><?php echo $name; ?></option>
                            <?php
                            } }?>
                    </select>

Yes, if that’s the selection criterion that you want to use.

You should also look into prepared statements instead of concatenating values into your query like that.

But it shows undefined index — i use in if(isset($_POST[‘fruitid’])){ $fruitid = $_POST[‘fruitid’]; select query here… } but undefined index fruitid in line 2 error ??

Is the field name correct in your form code? To guess what might be causing the error you mention, you probably need to post the modified code, and show us which line is throwing the error.

if(isset($_POST['fruitid'])){
$fruitid= $_POST['fruitid']; 
$getdata = mysqli_query($conn, "SELECT * FROM `ABC` WHERE `fruitid`= '$fruitid');
if(mysqli_num_rows($getdata) > 0){
                         while($data= mysqli_fetch_assoc($getdata)){
                          $tid   = $data['fruitid'];
                           $name = $data[' name'];
                    ?>      
                <option value="<?php echo $tid; ?>"><?php echo $name; ?></option>
                            <?php
                            } }?>
                    </select>

it fruitid undefined index— fruitid is the fk of the other table we have just id here of fruit table

As above.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.