FETCHING RECORDS FROM DROPDOWN AND INSERTING INTO DATABASE

 Code:

<?php
// Connect Database

$conn = mysqli_connect("localhost","root","","employee_store");

// Get all the category from the category table

$sql = "select * from category ";
$all_category = mysqli_query($conn,$sql);

if(isset($_POST['submit']))

{

    $name = mysqli_real_escape_string($conn,$_POST['PRODUCT_NAME']);

    $id = mysqli_real_escape_string($conn,$_POST['category_id_fk']);


    $sql_insert = "insert into product (PRODUCT_NAME, category_id_fk) values ('$name','$id')";
   

    if(mysqli_query($conn,$sql_insert))
    {
        echo '<script> alert("Product added Successfully") </script>';

    }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<form method="Post">
<label> Name of Product </label>
<input type="text" name="PRODUCT_NAME" required> <br>
<label> Select a Category </label>
<select name="category_id_fk">
<?php
while($category = mysqli_fetch_array($all_category,MYSQLI_ASSOC)):
?>
<option value="<?php echo $category["Category_Id"];?>">
<?php echo $category["Category_Name"]; // to show the category name
?>
</option>
<?php
endwhile; // while loop must be terminated..
?>
</select>
<br>
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>

Comments