简体   繁体   English

将数据提取到dropdownlist中并在php中获取所选数据

[英]fetching data into dropdownlist and get the selected data in php

I am trying to fetch some data from databse and display it into dropdownlist and get the selected data using php. 我试图从数据库中获取一些数据,并将其显示到dropdownlist中,并使用php获取选定的数据。

Code

<?php


    if(isset($_POST['action']) && $_POST['action'] == 'Save'){
        savecategory();

    } 

    function savecategory() {
        $category=$_POST["category"];


        $servername = "localhost";
        $username = "root";
        $password = "******";
        $dbname = "db";

        $conn = new mysqli($servername, $username, $password, $dbname);

        if (!conn) {
            die("Connection Failed: " . mysqli_connect_error());
        }
        echo"Connected Successfully";
        $sql = "INSERT INTO category_tbl(cat_name) VALUES ('$category')";
        if(mysqli_query($conn,$sql))
        {
            echo"Successfully Saved";

            }
            else{

                echo"save failed..!!";

                }       

    }


?>







<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>gallery category</title>
</head>
<body>
<form action="<?php $_SERVER["PHP_SELF"]?>" method="post">

<!--division for category insertion-->

<div class="categoryEntry">
<table align="center">
<th colspan="2">Gallery Category</th>
<tr>
<td>Category</td>

<td> <input type="text" name="category"> </td>

</tr>
<tr>
<td> <input type="submit" name="action" value="Save"> </td> <td> <input type="submit" name="action" value="Cancel"> </td>
</tr>
</table>
</div>

<!-- end of category insertion div-->



<!-- start retreive category data into table -->


<hr>
<br><br><br>
<div>
<table  align="center">
<th align="center" colspan="2"> Category List</th><br>
<tr><td>Select Your Category:</td>
<td><label>
<select name="Select" class="textfields" id="ddlcategory">
<option id="0">---Select your category---</option>
<?php 
$servername = "localhost";
        $username = "root";
        $password = "******";
        $dbname = "mydb";

        $conn = new mysqli($servername, $username, $password, $dbname);

        if (!conn) {
            die("Connection Failed: " . mysqli_connect_error());
        }
        echo"Connected Successfully";


$sql=mysqli_query("SELECT * FROM category_tbl");
while($category=mysqli_fetch_array($sql)){
?>

<option id="<?php echo $category['cat_id']; ?>">
<?php echo $category['cat_name']; ?></option>
<?php 
} 
?>
</select>
</label>
</td>
</tr>
</table>
</div>

</form>

</body>
</html>

I have write code for retrieving data but it can't able to show data in drop downlist. 我已经编写了用于检索数据的代码,但是它无法显示下拉列表中的数据。

help needed..!! 需要帮助..!! thanks.. 谢谢..

I hope the following code solves your problem: 我希望以下代码能解决您的问题:

<?php
{
    mysqli_select_db($conn, "db");
    $sql = "SELECT * FROM category_tbl";
    $query = mysqli_query($link1, $sql);
    echo"<select name='category_tbl'>";
    while($row = mysqli_fetch_array($query))
    {
        echo "<option value'" . $row['cat_id'] . "'>" . $row['cat_id'] . "</option>";
    }
    echo "</select>";
} 
?>

Try the below code 试试下面的代码

<select>
<?php
$query= "Select * from DB_Table_Name>";    //Your Sql Query in a variable
$execute = mysqli_query($db,$query);  // Execute your query..$db is your connection variable
while($row = mysqli_fetch_array($execute,MYSQLI_BOTH))
{?>
<option><?php echo $row['something']; ?></option> //Use Your Table Name Instead of Something
<?php
}
?>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM