Buat daftar drop-down yang opsinya diambil dari database MySQL di PHP

0
(0)

<?php

 

    

    $con = mysqli_connect("localhost","root","","example_store");

     

    

  

    

    $sql = "SELECT * FROM `category`";

    $all_categories = mysqli_query($con,$sql);

  

    

    

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

    {

        

        $name = mysqli_real_escape_string($con,$_POST['Product_name']);

        

        

        $id = mysqli_real_escape_string($con,$_POST['Category']);

        

        

        

        $sql_insert

        "INSERT INTO `product`(`product_name`, `category_id`)

            VALUES ('$name','$id')";

          

          

          

          

          

          if(mysqli_query($con,$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">    

</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">

            <?php 

                

                

                

                while ($category = mysqli_fetch_array(

                        $all_categories,MYSQLI_ASSOC)):; 

            ?>

                <option value="<?php echo $category["Category_ID"];

                    

                ?>">

                    <?php echo $category["Category_Name"];

                        

                    ?>

                </option>

            <?php 

                endwhile

                

            ?>

        </select>

        <br>

        <input type="submit" value="submit" name="submit">

    </form>

    <br>

</body>

</html>

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.