2011-08-10 139 views
2

我想知道你們是否可以幫助我。我有一個稱爲模塊的表,它內有moduleID,moduleTitle,moduleCode等。檢索通過的ID

複選框將顯示在每個模塊旁邊,複選框的值是moduleID。當用戶檢查模塊並點擊提交時,它們會被傳遞到另一個頁面上。在其他頁面上,我設法檢索已檢查過的模塊ID,但在轉換這些模塊ID時遇到問題,因此我可以顯示已在此新頁面上檢查過的模塊標題,模塊代碼等。

我用下面的代碼,但沒有運氣:困惑:

<?php 

$cats_upload = ""; 
    foreach($_POST['module'] as $cats) // errors if no modules are selected 
    { 
     echo '<p>Module data moduleID: '.$cats.'</p>'; 

    } 
     $sql = mysql_query("SELECT * FROM modules WHERE moduleID ='".$cats."'"); 


$productCount = mysql_num_rows($sql); //count the output amount 

if($productCount > 0){ 
    while($row = mysql_fetch_array($sql)){ 

    $moduleTitle = $row["moduleTitle"]; 


     $cats_upload .= "<tr><td>$moduleTitle</td></tr>"; 

    } 
} else { 
    $cats_upload = "<tr><td>No modules have been selected</td></tr>"; 
} 

?> 

如果任何人有什麼它可能是任何建議,請讓我知道。 感謝

回答

2
$cats_upload = ""; 

if (!empty($_POST['module'])) { 

    foreach ($_POST['module'] as $moduleID) { 
     $modules[] = $moduleID; 
    } 

    $module_list = implode(',', $modules); 

    $sql = "SELECT moduleTitle FROM modules WHERE moduleID IN (" . $module_list . ")"; 
    $result = mysql_query($sql) or die("Error in $sql: " . mysql_error()); 

    while ($row = mysql_fetch_array($result)) { 
     $cats_upload .= "<tr><td>" . $row['moduleTitle'] . "</td></tr>"; 
    } 

} else { 
    $cats_upload .= "<tr><td>No modules have been selected</td></tr>"; 
} 
1
$cats = array(); 
foreach($_POST['module'] as $cat) // errors if no modules are selected 
{ 
    echo '<p>Module data moduleID: '.$cat.'</p>'; 
    $cats[] = intval($cat); // Assumption: ids are numeric 
} 
$sql = mysql_query("SELECT * FROM modules WHERE moduleID IN (".implode(',', $cats).")"); 

這應該給你所有匹配模塊的SQL結果。