2016-02-18 141 views
0

我正在爲用戶定製報表。在此頁面中,我給出了用於從數據庫選擇數據的用戶複選框的選項。用戶選擇他想要查看的數據從table.My表有許多字段,如名稱,塊,聯繫......等等。如果用戶只想查看唯一的用戶名和姓名,那麼他只能查看名稱和電話框。 我複選框是從數據庫中選擇數據使用php中的複選框從mysql

 <form action="#" method="post"> 
<input type="checkbox" name="check_list[]" value="uid"><label>uid</label> 
<input type="checkbox" name="check_list[]" value="name"><label>name</label> 
<input type="checkbox" name="check_list[]" value="block"><label>block</label>....and so more 
<input type="submit" name="submit" value="Submit"/> 
</form> 

和PHP代碼我想是

if(isset($_POST['submit'])){ 

    $sql[] = "select "; 

    foreach($_POST['check_list'] as $selected){ 
     $sql[]=$selected.','; 
    } 

    $sql[]= " from emitra_basic LIMIT 5"; 

    foreach ($sql as $user) { 

     $ram="$user"; 
    } 

    if ($result=mysqli_query($conn, $ram)) 
    { 
     while ($row=mysqli_fetch_row($result)) 
     { 
      echo "id".$row['uid']."<br/>"; 
      echo "name".$row['name']."<br/>"; 
     } 
    } 
} 

但沒有result.what我應該嘗試一下呢?

+0

你能不能把UR創建表查詢。我們將如何測試,如果你不會把它呢? – rahul

+0

請重新格式化您的代碼。這是不可讀的。 –

+0

嘗試使用ajax或javascript,以便用戶選擇/取消選擇複選框時,結果將自動顯示,無需刷新頁面或單擊按鈕。 – rhavendc

回答

0

請嘗試

$sql[] = "select ".implode(', ', $_POST['check_list'])." from emitra_basic LIMIT 5"; 

,而不是

$sql[] = "select "; 
foreach($_POST['check_list'] as $selected){ 
$sql[]=$selected.',';} 
$sql[]= " from emitra_basic LIMIT 5"; 

得到的東西,如:

select uid, name, block from emitra_basic LIMIT 5; 

否則會有一個,在最後一個字段的末尾:

select uid, name, block, from emitra_basic LIMIT 5; -- This is not working! 
+0

請更多解釋輸出需要 – omnath

0

嘗試這樣的: - 沒有大的變化在您的編碼

if(isset($_POST['submit'])){ 

$sql=array(); 

$sql[] = "select "; 

$sql[]=join(",", $_POST['check_list']); 

$sql[]= " from emitra_basic LIMIT 5"; 

//print_r($sql); 

foreach ($sql as $user) { 

    //echo $user."<br>"; 

    $ram.=$user; 
} 

//echo $ram; exit; 

//select uid, from emitra_basic LIMIT 5 

if ($result=mysqli_query($conn, $ram)) 
{ 
    while ($row=mysqli_fetch_row($result)) 
    { 
     echo "id".$row['uid']."<br/>"; 
     echo "name".$row['name']."<br/>"; 
    } 
} 

}

0

試試這個在PHP

if(isset($_POST['submit'])){ 
$sql1 = "select "; 
$sql2 = ""; 
$checklist = $_POST['check_list']; 
$count = count($checklist); 
if($count!=1){ 
    $count = $count-1; 
    for ($i=0; $i < $count ; $i++) { 
     $sql2 = $sql2.$checklist[$i].","; 
    } 
    $sql2 = $sql2.$checklist[$count]; 
} else{ 
    $sql2 = $checklist[0]; 
} 
$sql3= " from emitra_basic LIMIT 5"; 
$totalSql = $sql1.$sql2.$sql3; 

if ($result=mysqli_query($conn,$totalSql)) 
    { while ($row=mysqli_fetch_row($result)) 
    { echo "id".$row['uid']."<br/>"; 
echo "name".$row['name']."<br/>";}} 

}

+0

此代碼還將導致類似'select,uid,name,block from ... '因爲第一個','有語法錯誤? –

+0

是的,我會修改 – sudhakar

+0

用我自己的邏輯修改 – sudhakar