Php 从选项中选择多个选项
所以我有一个选择器,它从数据库中获取信息 当我从选择器中选择某个内容并按:Php 从选项中选择多个选项,php,html,mysql,forms,mysqli,Php,Html,Mysql,Forms,Mysqli,所以我有一个选择器,它从数据库中获取信息 当我从选择器中选择某个内容并按:添加到列表时,它会生成一个包含所选信息的表格 现在这就是它应该做的。但现在,当我选择另一个结果并按添加到列表时。它删除旧的,并用新的替换。 但我实际上不希望它删除旧的一行,而是在它下面做一个新的行。所以那张桌子变大了。我该怎么做 选择器代码: <!--Selector--> <?php //Get name and id data from the db. In an assoc array
添加到列表
时,它会生成一个包含所选信息的表格
现在这就是它应该做的。但现在,当我选择另一个结果并按添加到列表时
。它删除旧的,并用新的替换。但我实际上不希望它删除旧的一行,而是在它下面做一个新的行。所以那张桌子变大了。我该怎么做 选择器代码:
<!--Selector-->
<?php
//Get name and id data from the db. In an assoc array
$results = $database->Selector();
echo "<form name='form' method='POST' id='selector'>";
echo "<select name='train_name' id='train_name' multiple='multiple'>";
// Loop trough the results and make an option of every train_name
foreach($results as $res){
echo "<option value=" . $res['train_name'] . ">" . $res['train_name'] . "</option>";
}
echo "</select>";
echo "<br />" . "<td>" . "<input type='submit' name='Add' value='Add to list'/>" . "</td>";
echo "</form>";
if(isset($_POST["train_name"])){
//Get all data from database, in an assoc array
$results = $database->getAllAssoc();
//Make table headers
?>
<div id="train_select_table">
<form name="selector" method="post" action="customer_list.php?user_id=<?php echo $_SESSION['user_id']?>">
<table>
<tr>
<th>Train name</th>
<th>Number of bogies</th>
<th>Number of axles</th>
<th>Delete</th>
<th>More info</th>
<th>Check</th>
<!--Only for admins (later!)-->
<!--<th>XML</th>
<th>SQL</th> -->
</tr>
<div id="looprow">
<?php
foreach($results as $res){
//Loop trough results, generate a tablerow every time
?>
<tr>
<td name="train_name"><?php echo $res['train_name']?></td>
<td><?php echo $res['number_of_bogies']?></td>
<td><?php echo $res['number_of_axles']?></td>
<td><a href="remove_from_table.php?train_id=<?php echo $res['train_id']?>">Delete</a></td>
<td><a href="expand_info.php?train_id=<?php echo $res['train_id']?>">More Information</a></td>
<td><input type="checkbox" name="checkbox[]" value="<?php echo $res['train_id']?>"></td>
<!--Only for admins (later!)-->
<!--<td><a href="convert_to_xml.php?train_id=<?php echo $res['train_id']?>">XML</a></td>
<td><a href="convert_to_sql.php?train_id=<?php echo $res['train_id']?>">SQL</a></td>-->
</tr>
<?php
}
?>
</div>
</table><br />
<input name="Add to list" type="submit" id="add_to_list" value="add_to_list">
</form>
</div>
<?php
}
?>
function getAllAssoc() {
$sql = "SELECT * FROM train_information WHERE train_name = :train_name";
$sth = $this->pdo->prepare($sql);
$sth->bindParam(":train_name", $_POST["train_name"]);
$sth->execute();
return $sth->fetchAll();
}
function selector() {
$sql = "SELECT train_name, train_id FROM train_information";
$sth = $this->pdo->prepare($sql);
$sth->execute();
return $sth->fetchAll();
}
我理解为什么它一直在取代旧的那排。这是因为我发送了一个新查询。但是我不知道如何保留旧值。因为每次用户都会选择一个值而不是多个值,所以如果您想同时使用新值和旧值,您应该使用
cookie
或会话
或该表单中的隐藏输入来存储旧值。这取决于你
例如,开始时,请执行以下操作:
<?php
session_start();//make it be before your output if you want to use session or cookie
if(!$_SESSION['train_name']) $_SESSION['train_name'] = array();
if(isset($_POST["train_name"])) $_SESSION['train_name'] = array_merge ($_SESSION['train_name'], $_POST["train_name"]);
.......
//next step, inquiry with $_SESSION['train_name']
只需将多选列表的名称从train\u name
更改为train\u name[]
,如下所示:
...
echo "<select name='train_name[]' id='train_name' multiple='multiple'>";
...
。。。
回声“;
...
通过这种方式,您的$\u POST['train\u name']
将是传递给bindParam