以下のPHPコードによって生成されたHTMLフォーム内にある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);
}
}
これが意味をなさない場合は、いくつかの画像などを作成しようとします。私は本当に、これについて正しい方法で進んでいるかどうか、またはそれが可能/より良い方法であるかどうかを尋ねているだけです. .