I want to add values in option tag from sql database with the help of php. I have tried it but it is not showing anything
Here is my code which I have tried
<form action="addsubcategory.php" method="post">
<input type="text" name="sub_category" placeholder="Add sub category">
<?php
$res = mysqli_query($conn, "SELECT * FROM product_category");
echo '<select name="testSelect" id="testId">';
//Here is the problem
while ($record = mysqli_fetch_array($res)) {
echo '<option value="'.$record['category_name'].'">' . "</option";
}
echo '</select>';
?>
</form>
Try with
echo '<option value="'.$record['category_name'].'">'.$record['category_name'].' "</option">';
Where you have this:
echo '<option value="'.$record['category_name'].'">' . "</option";
You need this:
echo '<option value="'.$record['category_name'].'">' . "</option>";
You have only added value to option tag.
while ($record = mysqli_fetch_array($res)) {
echo '<option value="'.$record['category_name'].'">' . "</option";
}
instead of above code you should use:
while ($record = mysqli_fetch_array($res)) {
echo '<option value='.$record["category_name"].'>'. $record["category_name"] .'</option>';
}
Now above code will add value and text in option tag. Text to view and value to set or get value from db.