我有一个 HTML 表单,其中一部分位于由以下 PHP 代码生成的 HTML 表单中:
<?php $rows = array(
array('weight' => 1000, 'cbm_min' => 0.1, 'cbm_max' => 2.3 ),
array('weight' => 1500, 'cbm_min' => 2.31, 'cbm_max' => 3.5 ),
array('weight' => 2000, 'cbm_min' => 3.51, 'cbm_max' => 4.6 ),
array('weight' => 2500, 'cbm_min' => 4.61, 'cbm_max' => 5.75 ),
array('weight' => 3000, 'cbm_min' => 5.75, 'cbm_max' => 6.9 )
);
?>
<form name="createtable" action="?page=createtable" method="POST" autocomplete="off">
<label for=tablename>Name of Table</label>
<input type=text name=tablename id=tablename>
<table border='1'>
<tr>
<th> Weight </th>
<th> CBM Min </th>
<th> CBM Max </th>
<th> Min </th>
</tr>
<?php
$i = 1; // I'll use this to increment the input text name
foreach ($rows as $row) {
// Everything happening inside this foreach will loop for as many records/rows that are in the $rows array.
?>
<tr>
<th> <?= (float) $row['weight'] ?> </th>
<th> <?= (float) $row['cbm_min'] ?> </th>
<th> <?= (float) $row['cbm_max'] ?> </th>
<th> <input type=text name="min<?= (float) $i ?>"> </th>
</tr>
<?php
$i++;
}
?>
</table>
<input type=submit value="Create Table">
</form>
当用户填写表单并提交时,内容将用于创建 mysql 表。这个想法是表单将复制创建的 mysql 表的样子。这很好用,但是现在我需要能够向表单表添加额外的“列”。
即,当用户按下按钮时,表单表中将出现另一列,因此用户可以填写值,然后该列将添加到传递给创建 mysql 表的 php 函数的数据中。
处理提交数据的php
<?php
if($page == "createtable"){
$tablename = $_POST['tablename'];
$mins = array($_POST['min1'],$_POST['min1'],$_POST['min2'],$_POST['min3'],$_POST['min4'],$_POST['min5'],$_POST['min6'],$_POST['min7'],$_POST['min8'],
$_POST['min9'],$_POST['min10'],$_POST['min11'],$_POST['min12'],$_POST['min13'],$_POST['min14'],$_POST['min15'],$_POST['min16'],$_POST['min17'],$_POST['min18'],
$_POST['min19'],$_POST['min20'],$_POST['min21'],$_POST['min22'],$_POST['min23'],$_POST['min24'],$_POST['min25'],$_POST['min26'],$_POST['min27'],$_POST['min28'],
$_POST['min29'],$_POST['min30']);
$log->createTable($tablename, $mins);
}
?>
用于创建 mysql 的函数 我知道这不能保护 mysql 我的注入攻击等 我只是创建它用于测试等,无论如何都需要重做它以进行新的更改
function createTable($tablename, $mins){
$con=mysqli_connect("localhost","****","****","****");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$tableCreate = "CREATE TABLE rates_{$tablename} (
Weight int(11),
CBMMin double,
CBMMax double,
Min double
)
";
$queryResult = mysqli_query($con, $tableCreate);
if ($queryResult === TRUE) {
print "<br /><br />Table Created";
$queryResult = mysqli_query($con, "INSERT INTO Custom_Rates (TableName) VALUES ('rates_{$tablename}');");
$rows = array(
array('weight' => 1000, 'cbm_min' => 0.1, 'cbm_max' => 2.3 ),
array('weight' => 1500, 'cbm_min' => 2.31, 'cbm_max' => 3.5 ),
array('weight' => 2000, 'cbm_min' => 3.51, 'cbm_max' => 4.6 ),
array('weight' => 2500, 'cbm_min' => 4.61, 'cbm_max' => 5.75 ),
array('weight' => 3000, 'cbm_min' => 5.75, 'cbm_max' => 6.9 ));
$i = 0;
foreach ($rows as $row) {
$value = $mins[$i];
if (empty($value)) {
$value = 0;
}
$queryResult = mysqli_query($con, "INSERT INTO rates_{$tablename} (Weight, CBMMin, CBMMax, Min) VALUES (".$row['weight'].",".$row['cbm_min'].",".$row['cbm_max'].",".$value.");");
if ($queryResult === TRUE){
}else{
print "<br /><br />No Row created. Check " . mysqli_error($con);
}
$i++;
}
} else {
print "<br /><br />No TABLE created. Check " . mysqli_error($con);
}
}
如果这没有任何意义,请说,我会尝试绘制一些图像等。我真的只是在问我是否正在采取正确的方式,或者是否有可能/更好的方式去做.