所以我有一个基于我的数据库的选择器。一旦人们从该选择器中选择了某些内容,请选择它并按下一步。他们会看到这个页面:
(此处示例中选择train02)
此代码为:
<form method="POST" action="add_to_summary.php?user_id=<?php echo $_SESSION['user_id']?>">
<div id="customer_list_table">
<table>
<tr>
<th>Train id</th>
<th>Train Name</th>
<th>Tare Weight</th>
<th>Numbers of bogies</th>
<th>Numbers of axles</th>
</tr>
<?php
foreach($_POST["checkbox"] as $key=>$val){
$data = $database->getAllAssoc_id($val);
foreach($data as $data1) { ?>
<tr>
<input type='hidden' name='user_id[<?php echo $_GET['user_id']?>]' value='<?php echo $_GET['user_id']?>'>
<input type='hidden' name='train_id[<?php echo $data1['train_id']?>]' value='<?php echo $data1['train_id']?>'>
<td><?= $data1['train_id'] ?></td>
<td><?= $data1['train_name'] ?></td>
<td><?= $data1['tare_weight'] ?></td>
<td><?= $data1['number_of_bogies'] ?></td>
<td><?= $data1['number_of_axles'] ?></td>
</tr><?php
}
} ?>
</table>
</div>
<input name="Add to list" type="submit" id="add_to_list" value="Add to list">
</form>
当他们按添加到列表时,插件会像这样发送到我的数据库:
function summary_add($id) {
$sql = "INSERT INTO user_train_information "
. "(train_id, user_id)"
. "VALUES (:train_id, :user_id) ";
$sth = $this->pdo->prepare($sql);
$sth->bindParam(':train_id', $id, PDO::PARAM_STR);
$sth->bindParam(':user_id', $id, PDO::PARAM_STR);
$sth->execute();
}
add_to_summary.php
上的代码:
<?php
foreach($_POST['user_id'] as $id) {
$test = $database->summary_add($id);
}
?>
现在,信息将被插入我的数据库中。但是我选择的那个加倍。像这样:
当我将$_POST['user_id']
更改为$_POST['train_id']
时,它会输入 14 2次。
当我执行foreach($_POST as $id)
时,我得到2个数组到字符串转换错误。
我如何解决这个问题?
我希望表格正确插入,以便user_id
列 1 ,train_id
列 14
的 编辑:
对于选择器/选择框:form pass selected to next page
编辑:
它现在正在运作!
改变了什么:
更改为隐藏字段:
<input type='hidden' name='user_id' value='<?php echo $_GET['user_id']?>'>
<input type='hidden' name='train_id' value='<?php echo $data1['train_id']?>'>
功能:
function summary_add($train_id, $user_id) {
$sql = "INSERT INTO user_train_information "
. "(train_id, user_id)"
. "VALUES (:train_id, :user_id) ";
$sth = $this->pdo->prepare($sql);
$sth->bindParam(':train_id', $train_id, PDO::PARAM_STR);
$sth->bindParam(':user_id', $user_id, PDO::PARAM_STR);
$sth->execute();
}
我插入的地方:
<?php
$test = $database->summary_add($_POST['train_id'], $_POST['user_id']);
print_r($_POST);
?>
答案 0 :(得分:1)
function summary_add($train_id, $user_id) {
$sql = "INSERT INTO user_train_information "
. "(train_id, user_id)"
. "VALUES (:train_id, :user_id) ";
$sth = $this->pdo->prepare($sql);
$sth->bindParam(':train_id', $train_id, PDO::PARAM_STR);
$sth->bindParam(':user_id', $user_id, PDO::PARAM_STR);
$sth->execute();
}
add_to_summary.php上的代码:
<?php
$test = $database->summary_add($_POST['train_id'][0], $_POST['user_id'][0]);
?>
摘要:您必须将train_id和user_id放在summary_add
函数中。