データベースからデータを取得し、ドロップダウンリストに表示して、選択したデータをPHPで取得しようとしています。ドロップダウンリストにデータを取得し、選択したデータを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>
私は、データを取得するための書き込みのコードを持っているが、それはドロップdownlist内のデータを表示することができないことができます。
助けが必要です!!おかげで..
を試してみてください。 gallery_tblには、gal_id、filename、uploaded_time、ca_idの4つの列があります。 cat_idはgallery_tblの外部キーです。私はPHPで非常に新しいです。 – Student