2012-01-19 13 views
0

データベースからのデータを含むテーブルを(Webページ内に)表示したいが、ユーザが選択したフィールドを持つテーブルだけを表示したい。例:テーブルに合計6つのフィールド(名前、電子メール、年齢、場所、性別、職業)があります。私のページのユーザーが名前、電子メール、年齢を選択したとします。すべての行エントリを含むが、指定されたフィールドのみを含むテーブルを表示したい。mySqlテーブルの指定されたフィールドのみからデータを取得する方法はありますか?

これは私がこれまでに書いたものですが、動作していないようです。

include("init.php"); 

$fields = $_POST["fields"]; 
$sorting = trim($_POST["sorting"]); 
$filter = trim($_POST["filter"]); 

$sql_query = sprintf("select * from applicants where %s order by %s", $filter, $sorting); 
$query = mysql_query($sql_query); 
$num_rows = mysql_numrows($query); 

echo "<table border='1'>"; 
echo "<tr>"; 
    foreach($fields as $field) { 
     $field = ucfirst($field); 
     echo "<th>$field</th>"; 
    } 
echo "</tr>"; 

for($i = 0; $i < $num_rows; $i++) { 
    echo "<tr>"; 
    while($field = mysql_fetch_field($query)) { 
     if(in_array($field->name, $fields)) { 
      $data = mysql_result($query, $i, $col); 
      echo "<td>$data</td>"; 
     } else { 
      echo "<td>false</td>"; 
     } 
    } 
    echo "</tr>"; 
} 

echo "</table>"; 
+0

PHPコア関数にmysql_numrows関数が存在しないとエラーがあると思っていました。 –

答えて

1

<option>を列名の値に設定してからこれを使用する必要がありますコード:

include('init.php'); 

$fields = $_POST['fields']; 
$sorting = trim($_POST['sorting']); 
$filter = trim($_POST['filter']); 

function filterFields($field) { 
    if (in_array($field, array(/* Here you need to add the fields you allowed to post, to prevent attack */))) { 
    return '`' . $field . '`'; 
    } else { 
    return 'NULL'; 
    } 
} 

$escapedFields = array_map('filterFields', $fields); 

// Make the query, here we use 'implode' function to join all array with ',' 
// Example if we have array('name', 'time'), then the function will return 'name,time' 
$query = mysql_query('SELECT ' . implode(',', $escapedFields) . ' FROM `applicants` WHERE ' . $filter . ' ORDER BY ' . $sorting); 
// If the query return something, then... 
if (mysql_num_rows($query)) { 
    echo '<table border='1'> 
<tr>'; 
    // Here we print the table header. 
    for ($i = 0, $fieldsLength = sizeof($field); $i < $fieldsLength; ++$i) { 
    echo '<th>' . ucfirst($fields[$i]) . '</th>'; 
    } 
    echo '</tr>'; 

    // Here we print the result. 
    while ($result = mysql_fetch_assoc($query)) { 
    echo '<tr>'; 
    $resultKeys = array_keys($result); 
    for ($i = 0, $resultKeysLength = sizeof($resultKeys); $i < $resultKeysLength; ++$i) { 
     echo '<td>' . $result[$resultKeys[$i]] . '</td>'; 
    } 
    echo '</tr>'; 
    } 
    echo '</table>'; 
} 
+0

このシナリオはSQLインジェクションのために熟しています!ユーザーが入力した情報は、常にサニタイズしてください。 –

+0

@BenDはい、私は注射を防ぐために編集しています。 –

1

これを行う最も簡単な方法は、

select * from applicants 

SQLクエリを変更することであろう。 *は「すべての列」を意味します。あなたは$ _POST [ 'フィールド']変数からこれを簡単に取得することができます

$sql_query = sprintf("select `name`, `email`,`age` from applicants where %s order by %s", $filter, $sorting); 
$query = mysql_query($sql_query); 
$num_rows = mysql_numrows($query); 

だけで:に変更します(あなたがそれをエスケープしていることを確認すること!):

"select `".implode('`,`',$escaped_fields_array)."` from applicants 

また、ちょうど私$ _POST ['fields']変数が連想配列(類似のもの)として渡されたことを確認しておきたい

+0

私はそれを理解していますが、私の場合、ユーザーが選択したフィールドはわかりません。私は、ユーザーが選択したフィールドを含む$ fields配列変数を持っています。この配列には1〜6個のフィールドが含まれています。 –

+0

$ _POST ['fields']変数が配列であることを確認するために、質問を編集しましたか?それをテストするには、die(print_r($ _ POST ['fields']))を確認して、わからない場合は –

+0

はい、$ _POSTが配列として渡されます。これが私のやり方です。