【发布时间】:2015-07-24 20:49:20
【问题描述】:
所以我有一个从数据库中获取信息的选择器。
当我从选择器中选择某些内容并按下:Add to list 时,它会生成一个包含所选信息的表格。
现在这是它应该做的。但是现在,当我选择另一个结果并按Add to list 时。它会删除旧的并用新的替换它。
但我实际上不希望它删除旧的,而是在它下面创建一个新行。所以那张桌子变大了。我该怎么做?
选择器代码:
<!--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();
}
我明白为什么它不断替换旧行。这是因为我发送了一个新查询。但我不知道如何保留旧的。
【问题讨论】:
标签: php html mysql forms mysqli