根据变量获取字段数
get number of fields based on variable
我的插入内容如下所示:
function train_add() {
$sql = "INSERT INTO train_information "
. "(train_name, tare_weight, number_of_bogies, number_of_axles, wheel_diameter_min, wheel_diameter_max)"
. "VALUES (:train_name, :tare_weight, :number_of_bogies, :number_of_axles, :wheel_diameter_min, :wheel_diameter_max) ";
$sth = $this->pdo->prepare($sql);
$sth->bindParam(':train_name', $_POST['train_name'], PDO::PARAM_STR);
$sth->bindParam(':tare_weight', $_POST['tare_weight'], PDO::PARAM_STR);
$sth->bindParam(':number_of_bogies', $_POST['number_of_bogies'], PDO::PARAM_STR);
$sth->bindParam(':number_of_axles', $_POST['number_of_axles'], PDO::PARAM_STR);
$sth->bindParam(':wheel_diameter_min', $_POST['wheel_diameter_min'], PDO::PARAM_STR);
$sth->bindParam(':wheel_diameter_max', $_POST['wheel_diameter_max'], PDO::PARAM_STR);
$sth->execute();
}
我通过表格插入它:
<div id='toevoegen_trein'>
<form action="add_train_send.php" method="post">
<table>
<tr>
<td>Train Name:</td>
<td><input type="text" name="train_name" required><br></td>
</tr>
<tr>
<td>Tare Weight:</td>
<td><input type="text" name="tare_weight" required><br></td>
</tr>
<tr>
<td>Number of Bogies:</td>
<td><input type="text" name="number_of_bogies" required><br></td>
</tr>
<tr>
<td>Number of Axles:</td>
<td><input type="text" name="number_of_axles"><br></td>
</tr>
<tr>
<td>Wheel Diameter Minimal:</td>
<td><input type="text" name="wheel_diameter_min"><br></td>
</tr>
<tr>
<td>Wheel Diameter Maximal:</td>
<td><input type="text" name="wheel_diameter_max"><br></td>
</tr>
<tr>
<td><input type="submit" name="toevoegen" value="Next"></td>
</tr>
</table>
</form>
</div>
add_train_send.php:
<?php
$add_train = $database->train_add();
?>
现在。当我在表单上按提交时,它会转到 add_train_send.php。
我希望该页面不仅将信息发送到数据库。但我也想获得轴数。 (然后是他们身后的填充字段)。
现在我不知道该怎么做。因为如果轴数是 6,我只希望页面创建 6 个字段。如果轴数是 12,我希望它创建 12 个字段。
我该怎么做?
编辑:这是我希望它看起来如何的示例:
也在我的数据库中,我有一个 table 命名为:axle
内容:
train_id
axle_id
axle
distance
它已经有一个触发器,所以当我插入一个有 12 个轴的火车时,它使 12 增加了 12 个轴,我需要做的就是填写距离(参见 .png 示例)
如果我理解正确,您想显示 X 数量的文本字段(或任何输入类型。X 是用户在轴数)
为此,您需要获取您已有的轴数的值:$_POST['number_of_axles']
之后,您将经历 X 次 for 循环。我还注意到您的轴数不是必需的,但其他一些字段是必需的。所以你首先需要检查是否 $_POST['number_of_axles']
isset
。所以你这样开始:
if(isset($_POST['number_of_axles'])){
for($i=0; $i<$_POST['number_of_axles']; $i++){
echo "<input type='text' name='axles[$i]'>";
}
}
现在您在 for 循环之外开始表单并在 for 循环之外结束它。
编辑:
另外我忘了提到 name
部分。我这样声明名称:name='axles[$i]'
以确保每个文本字段都有不同的名称。现在,当您检查 $_POST['axles']
时。你会得到一个类似于:
的数组
[1]=>'input_of_axle1'
[2]=>'input_of_axle2'
[3]=>'input_of_axle3'
现在你可以循环遍历它了。像这样:
$axles = $_POST['axles'];
foreach($axles as $axle){
echo 'This is an axle:'.$axle; // I have no idea what an axle is
}
我的插入内容如下所示:
function train_add() {
$sql = "INSERT INTO train_information "
. "(train_name, tare_weight, number_of_bogies, number_of_axles, wheel_diameter_min, wheel_diameter_max)"
. "VALUES (:train_name, :tare_weight, :number_of_bogies, :number_of_axles, :wheel_diameter_min, :wheel_diameter_max) ";
$sth = $this->pdo->prepare($sql);
$sth->bindParam(':train_name', $_POST['train_name'], PDO::PARAM_STR);
$sth->bindParam(':tare_weight', $_POST['tare_weight'], PDO::PARAM_STR);
$sth->bindParam(':number_of_bogies', $_POST['number_of_bogies'], PDO::PARAM_STR);
$sth->bindParam(':number_of_axles', $_POST['number_of_axles'], PDO::PARAM_STR);
$sth->bindParam(':wheel_diameter_min', $_POST['wheel_diameter_min'], PDO::PARAM_STR);
$sth->bindParam(':wheel_diameter_max', $_POST['wheel_diameter_max'], PDO::PARAM_STR);
$sth->execute();
}
我通过表格插入它:
<div id='toevoegen_trein'>
<form action="add_train_send.php" method="post">
<table>
<tr>
<td>Train Name:</td>
<td><input type="text" name="train_name" required><br></td>
</tr>
<tr>
<td>Tare Weight:</td>
<td><input type="text" name="tare_weight" required><br></td>
</tr>
<tr>
<td>Number of Bogies:</td>
<td><input type="text" name="number_of_bogies" required><br></td>
</tr>
<tr>
<td>Number of Axles:</td>
<td><input type="text" name="number_of_axles"><br></td>
</tr>
<tr>
<td>Wheel Diameter Minimal:</td>
<td><input type="text" name="wheel_diameter_min"><br></td>
</tr>
<tr>
<td>Wheel Diameter Maximal:</td>
<td><input type="text" name="wheel_diameter_max"><br></td>
</tr>
<tr>
<td><input type="submit" name="toevoegen" value="Next"></td>
</tr>
</table>
</form>
</div>
add_train_send.php:
<?php
$add_train = $database->train_add();
?>
现在。当我在表单上按提交时,它会转到 add_train_send.php。 我希望该页面不仅将信息发送到数据库。但我也想获得轴数。 (然后是他们身后的填充字段)。 现在我不知道该怎么做。因为如果轴数是 6,我只希望页面创建 6 个字段。如果轴数是 12,我希望它创建 12 个字段。
我该怎么做?
编辑:这是我希望它看起来如何的示例:
也在我的数据库中,我有一个 table 命名为:axle
内容:
train_id
axle_id
axle
distance
它已经有一个触发器,所以当我插入一个有 12 个轴的火车时,它使 12 增加了 12 个轴,我需要做的就是填写距离(参见 .png 示例)
如果我理解正确,您想显示 X 数量的文本字段(或任何输入类型。X 是用户在轴数)
为此,您需要获取您已有的轴数的值:$_POST['number_of_axles']
之后,您将经历 X 次 for 循环。我还注意到您的轴数不是必需的,但其他一些字段是必需的。所以你首先需要检查是否 $_POST['number_of_axles']
isset
。所以你这样开始:
if(isset($_POST['number_of_axles'])){
for($i=0; $i<$_POST['number_of_axles']; $i++){
echo "<input type='text' name='axles[$i]'>";
}
}
现在您在 for 循环之外开始表单并在 for 循环之外结束它。
编辑:
另外我忘了提到 name
部分。我这样声明名称:name='axles[$i]'
以确保每个文本字段都有不同的名称。现在,当您检查 $_POST['axles']
时。你会得到一个类似于:
[1]=>'input_of_axle1'
[2]=>'input_of_axle2'
[3]=>'input_of_axle3'
现在你可以循环遍历它了。像这样:
$axles = $_POST['axles'];
foreach($axles as $axle){
echo 'This is an axle:'.$axle; // I have no idea what an axle is
}