2013-03-13 47 views
0

這是我在這裏的原題的第2部分:我想根據檢查框的值顯示來自數據庫的結果。有任何想法嗎?

Any ideas why results of query are not being displayed as Checkboxes?

對用戶呈現與價值的複選框。

我們希望看到基於複選框顯示的結果。

如果用戶選中一個框,則會顯示與該複選框關聯的值。

類似地,如果用戶檢查多個選中的框,則以逗號分隔的方式顯示選中框的值。

複選框標記已成功實施。

的代碼工作正常,如下所示:

<form name ="checkForm" method="post" action="Search.php"> 
    <input type="checkbox" name="ckform1" 
     onClick="checkAll(this.form,this)">Check/Uncheck All 
     <br> 
<?php 

    // Connect to SQL Server database 
    include("../dbConnect.php"); 


    // Construct query 
    $sql = "SELECT * FROM Search WHERE resultType IS NOT NULL"; 

// Execute query 
$stmt = sqlsrv_query($conn, $sql); 
if($stmt === false) 
{ 
    echo "Error in executing query.</br>"; 
    die(print_r(sqlsrv_errors(), true)); 
} 

    while($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)){ 
    echo '<input type="checkbox" name="ck[]" 
    value="'.$row["resultType"].'"><font class=heading>'.$row["resultType"].'</font><br>'; 
    } 
    // Free statement and connection resources 
    sqlsrv_free_stmt($stmt); 
    sqlsrv_close($conn); 
    ?> 
    <P><input type="submit" value="Go"> 
    </form> 

現在,我有search.php中困難。

這是應根據選中的複選框或選中的複選框返回值的文件。

到目前爲止,我沒有收到錯誤。相反,我得到一個空白屏幕。

感謝您的幫助

<?php 

$ckList = format($_GET["ck"]); 

// Connect to SQL Server database 
include("../dbConnect.php"); 


    if (isset($_POST) && isset($_POST['Search'])) 
    { 
    $resultTypeWhereClause = ''; 

    if (! empty($_POST['ckList'])) // if the ckoptions var is set, we'll add the values to the query 
    { 
     $optionsToSearch = is_array($_POST['ckList']) ? $_POST['ckList'] : array($_POST['ckList']); 
     foreach ($optionsToSearch as &$ckval) 
     { 
       // prevent sql injection 
      $ckval = mysql_real_escape_string($ckval); 
     } 
     // add all values to an 'IN' clause 
     $resultTypeWhereClause = "resultType IN ('" . implode("', '", $optionsToSearch) . "')"; 
    } 

    if (empty($resultTypeWhereClause)) 
    { 
     $resultTypeWhereClause = '1'; 
    } 

    $found = array(); 
    // build the final query 
    $sql = 'SELECT * FROM Search WHERE ' .$resultTypeWhereClause; 

    // Execute query 
    $stmt = sqlsrv_query($conn, $tsql); 
    if($stmt === false) 
    { 
    echo "Error in executing query.</br>"; 
    die(print_r(sqlsrv_errors(), true)); 
    } 
    $results = array(); 

// Retrieve and display the results of the query 
$lastresultType = ""; 
while($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)) { 
    array_push($results,$row); 
} 

echo json_encode($results); 

// Free statement and connection resources 
sqlsrv_free_stmt($stmt); 
sqlsrv_close($conn); 
    } 
?> 

回答

0

是在正確的地方數據庫你嘗試過或死亡(),這也給你一個錯誤?

+0

對兩者都是。 雖然仍然有問題,雖然 – 2013-03-14 18:13:19

相關問題