将数据提取到下拉列表中并在php中获取所选数据

时间:2016-05-11 19:28:42

标签: php html mysqli drop-down-menu data-retrieval

我正在尝试从数据库中获取一些数据并将其显示到下拉列表中并使用php获取所选数据。

  

代码

<?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>

我已经编写了用于检索数据的代码,但它无法在下拉列表中显示数据。

需要帮助.. !!感谢..

2 个答案:

答案 0 :(得分:0)

我希望以下代码能解决您的问题:

<?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>";
} 
?>

答案 1 :(得分:0)

尝试以下代码

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