2016-09-22 27 views
-1

我已成功連接到MSSQL數據庫,並試圖將數據從數據庫導入到HTML表單的下拉框中。我將如何通過SQL查詢將數據庫數據導入到該下拉列表中?我的表名是Stage_One,我想從表中拉場MR_Name將SQL Server數據導入HTML下拉框

這是我的聯繫信息...

<?php  
$serverName = "xxxxxxxxxx"; 
$uid = "xxxx";  
$pwd = "xxxxxxxxxxx";  
$databaseName = "Stage"; 

$connectionInfo = array("UID"=>$uid,        
         "PWD"=>$pwd,        
         "Database"=>$databaseName); 

/* Connect using SQL Server Authentication. */  
$conn = sqlsrv_connect($serverName, $connectionInfo); 

if ($conn)  
{  
    echo "Statement executed.<br>\n";  
}  
else  
{  
    echo "Error in statement execution.\n";  
    die(print_r(sqlsrv_errors(), true));  
} 
?> 

這是我爲我的HTML代碼下拉...

<section class="desc-block-left0"> 
<div align="center"> 
<div id="vendor"> 
<strong>Vendor:</strong> 
</div> 

<select name="vendor_dropdown"> 
<option value=""></option> 
</select> 
</div> 
</section> 
+0

你張貼了這個已經http://stackoverflow.com/questions/39641798/import-mssql-data-into-html-dropdown - 請不要轉貼。 –

回答

0
//set the query <br> 
$version = mssql_query('SELECT @@VERSION'); 
//fetch the array 
<?php while($row=mssql_fetch_array($version)){ 
        $id=$row["name_row_1"]; 
        $nom=$row["name_row_2"]; 
        ... 
//here just echo the result of the rows inside the html tags 
        echo "<option value={$id}>{$nom}</option>"; 
        } ?> 
0

您可以使用SQL查詢從數據庫中選擇列並使用如下while循環html標記檢索:

<section class="desc-block-left0"> 
    <div align="center"> 
    <div id="vendor"> 
    <strong>Vendor:</strong> 
    </div> 

    <select name="vendor_dropdown"> 

<?php 
$query="SELECT MR_Name FROM Stage_One"; 
$result=mysqli_query($con,$query); 

while($res=mysqli_fetch_array($result, MYSQLI_ASSOC)) 
{ 
?> 

<option value=""><?php echo $res['MR_Name']; ?></option> 

<?php } ?> 


</select> 
</div> 
</section> 

,如果你使用的是MSSQL,請試試這個:

<section class="desc-block-left0"> 
     <div align="center"> 
     <div id="vendor"> 
     <strong>Vendor:</strong> 
     </div> 

     <select name="vendor_dropdown"> 

<?php 
$sql = "SELECT MR_Name FROM Stage_One"; 
$stmt = sqlsrv_query($conn, $sql); 
if($stmt === false) { 
    die(print_r(sqlsrv_errors(), true)); 
} 

while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { 
     ?> 

     <option value=""><?php echo $res['MR_Name']; ?></option> 
<?php } 

sqlsrv_free_stmt($stmt); 
?> 

</select> 
    </div> 
    </section> 

不要忘了,包括您的數據庫連接文件或代碼。

+0

所以我做了這些改變,每當我點擊下拉框,什麼也沒有發生......任何想法爲什麼? – Rataiczak24

+0

您沒有從數據庫獲取值。只需使用<?php mysqli_num_rows($ result); ?>來回顯來自數據庫的行數。 –

+0

你可以在你的代碼中更新嗎,這樣我就可以看到它應該是什麼樣子了? – Rataiczak24

0

這是一個簡單的例子,我用從我的數據庫中檢索數據

$sql = "SELECT person FROM table where person='test'"; 
$result2 = $conn->query($sql); 
    if (!$conn->query($sql)) { 
    printf("Errormessage: %s\n", $conn->error); 
    } 
    else{ 
    $result = $conn->query($sql); 
    if ($result2->num_rows > 0) { 
    // output data of each row 
    echo "<table>"; 
    echo"<tr><th>person</th>/th></tr>"; 
    while($row = $result2->fetch_assoc()) { 
    echo"<tr class=".$dif.">"; 
     if ($dif=="one") $dif="two";else $dif="one"; 
     echo "<td >"; 
     echo $row["person"]. "</td>"; 
     echo"</td>"; 
     echo"</tr>"; 
     } 
     echo "</table>"; 
+0

你意識到OP不使用MySQLi_ API,而是使用MSSQL。這裏有兩種不同的動物 –

相關問題