2016-05-11 7 views
0

データベースからデータを取得し、ドロップダウンリストに表示して、選択したデータを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内のデータを表示することができないことができます。

助けが必要です!!おかげで..

答えて

0
------------ hope down code is your solution----------- 
<?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>"; 
} 
?> 
+0

を試してみてください。 gallery_tblには、gal_id、filename、uploaded_time、ca_idの4つの列があります。 cat_idはgallery_tblの外部キーです。私はPHPで非常に新しいです。 – Student

0

ええ、それが正常に動作しますが、私は選択した項目の値を取得し、gallery_tblに挿入する以下のコード

<select name="supplier" > 
<?php 
    include ('../connect.php'); // Include your connection 
    $result = $db->prepare("YOUR QUERY HERE");  // Run your query 
    $result->execute(); // Execute your query 
    // Loop through the query results, outputing the options one by one 
    for($i=0; $row = $result->fetch(); $i++){ 
    ?> 
    //Enter Your Database Table Column name instead of something 
    <option><?php echo $row['something']; ?></option> 
<?php 
    } 
    ?> 
    </select> 
+0

あなたは前に

関連する問題