0

ユーザーが知っている情報を送信し、返してほしい情報を選択できる Web ページを作成しようとしています。たとえば、申請の ID 番号がわかっている場合は、それを入力して、返された値が ID 番号と製品名になるように要求できます。

取得される情報は MySQL データベースに保存され、html ページは選択メニュー、テキスト ボックス、およびチェックボックスでレイアウトされます (ユーザーが名前を選択したり、他の情報を入力したり、チェックボックスを使用して選択したりできるように)返してもらいたいその他の情報)。

関連するhtmlは次のとおりです。

<table><tr>
<form name="input" method="post" action="next.php">
<td width="120">Flowcell ID:<br />
<select name = "Flowcell_ID">
<option disabled="disabled" selected="selected">
Select...
</option>
<?php
$link = mysqli_connect("localhost","user","pass","db");
$query = "SELECT DISTINCT ID FROM table";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)){
$id = $row['ID'];
echo "<option value=\"$id\">$id</option>";
}
mysqli_close($link);
?>
</select></td>
</tr><tr>
<td width="120">Name:<input type="text" name="Name" /></td>
</tr><tr>
<td width="120">Date:<input type="text" name="Date" /></td>
</tr><tr>
<td width="120">Group:<input type="text" name="Group" /></td>
</tr><tr>
<td width="120"><input type="checkbox" name="options[]" value="ID">ID</input></td>
<td width="120"><input type="checkbox" name="options[]" value="Name">Name</input></td>
<td width="120"><input type="checkbox" name="options[]" value="Date">Date</input></td>
<td width="120"><input type="checkbox" name="options[]" value="Group">Group</input></td>
</tr></table>

私がこれまでに持っている唯一のphpは次のとおりです。

$id = $_POST['ID'];
$name = $_POST['Name'];
$date = $_POST['Date'];
$group = $_POST['Group'];

次のような MySQL クエリを作成するにはどうすればよいですか

SELECT [checked checkboxes] FROM table WHERE [information field] = [user-entered information]

?

ありがとう!

4

1 に答える 1

0

クエリを段階的に作成できます。

$fields = array(
   'ID'   => 'id',
   'Name' => 'name',
   'Date' => 'date',
   'Group' => 'group',
);

$query = 'SELECT';  // Optional fields to be displayed may go here
$comma = ' ';       // and if they do, start with $comma = ', ';

$where = array();

foreach($fields as $post => $mysql)
{
    // here we have $post equal to 'ID', our POST field, and
    // $mysql equal to, say, 'Id' -- the MySQL field name.
    // Check whether options['ID'] is set. If so, we must get
    // the _POST[ID] field and check its contents against "Id" field in DB.
    if (in_array($post, $_POST['options']))
    {
        $query .= "$comma$mysql";
        $comma = ', ';
        // Add WHERE condition
        $where[] = "$mysql = '" . mysql_real_escape_string($_POST[$post]) . "'";
    }
}
// $comma, the separator between option fields, also doubles up as a check
// to see whether we have conditions.
// another possibility would be, "if (empty($where))"
if (' ' == $comma)
   die("You must select at least one checkbox!");

$query .= ' FROM table WHERE ';
// Build WHERE condition
$query .= '(' . implode(' AND ', $where).')';

// Other conditions may go here (e.g. " AND record_valid = 1 ")

$query .= ';';
于 2012-07-09T18:05:06.550 に答える