各位大家好!
当我们将html表单数据插入到sql表中时,我们在处理程序文档中有这个变量列表:
$date = $_POST['date'];
$amount = $_POST['amount'];
$number = $_POST['number'];
等等。
现在 - 我有一个锯木厂,生产不同规格的木材。这些规范存储在sql表中。我可以使用html表单添加新的(例如松木板19mm * 100mm * 3m将在表格中创建一个' p191003'条目)。 然后将用作另一种形式的输入字段名称,其中我有一个包含所有大小和输入字段的表来输入金额(生成的日志数)。每天将生成的数量提交给另一个sql表。 我的问题是,我需要每增加一个尺寸才会出现'在变量列表中能够将生成的数量提交给数据库。 如果我手动完成,它将如下所示:
$p191003 = $_POST['p191003'];
$p1910024 = $_POST['p1910024'];
$p221003 = $_POST['p221003'];
$p227024 = $_POST['p227024'];
$p22703 = $_POST['p22703'];
$p221453 = $_POST['p221453'];
$p291003 = $_POST['p291003'];
EDIT1:
这是我将规范添加到数据库的方式:
<form action="proc_spec.php" method="post" />
<input type="radio" onclick="javascript:pCheck();" name="type" id="sp" value="Spruce/pine">Spruce/pine
<br>
<input type="radio" onclick="javascript:pCheck();" name="type" id="bir" value="Birch">Birch
<br>
<input type="radio" onclick="javascript:pCheck();" name="type" id="asp" value="Aspen">Aspen
<br><br>
<table><tr>
<td>Height (мм):</td><td>Width (мм): </td><td>Length (м): </td>
</tr><tr>
<td><input type="height" name="height" /></td><td><input type="width" name="width" /></td><td><input type="length" name="length" /></td>
</tr>
</table>
<br><br><br><br>
<input type="image" src="images/confirm.png" class="pic" alt="Confirm" />
</form>
这是处理程序:
//connect to db
$type = $_POST['type'];
$height = $_POST['height'];
$width = $_POST['width'];
$length = $_POST['length'];
$h = $height * 0.001;
$w = $width * 0.001;
$l = $length;
$v = $h * $w * $l;
$lvar = $length * 10;
// begin the transaction
$conn->beginTransaction();
// our SQL statememtns
if ($type == 'sp') {
$conn->exec("INSERT INTO Rspecs (name, volume, type, scode) VALUES ('$h×$w×$l', '$v', '$type', 'sp$height$width$lvar')");
}
if ($type == 'bir') {
$conn->exec("INSERT INTO Rspecs (name, volume, type, scode) VALUES ('$h×$w×$l', '$v', '$type', 'bir$height$width$lvar')");
}
if ($type == 'asp') {
$conn->exec("INSERT INTO Rspecs (name, volume, type, scode) VALUES ('$h×$w×$l', '$v', '$type', 'asp$height$width$lvar')");
}
// commit the transaction
$conn->commit();
echo "Added to database";
include("views/add_processing.php");
} //disconnect
这是添加生成大小的表单(php部分):
//connect
$query = "SELECT type, name, volume, speccode FROM Rspecs GROUP by type";
$result = mysql_query($query);
while( ($row = mysql_fetch_array($result)))
{
echo "<tr>";
echo "<td>".$row['type']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['volume']."</td>";
echo "<td><input type='amount' name='".$row['scode']."' /></td>";
echo "</tr>";
}
// disconnect from the database
这是我之前使用的处理程序(当我必须手动输入尺寸时)
$p191003 = $_POST['p191003'];
$p1910024 = $_POST['p1910024'];
$p221003 = $_POST['p221003'];
$p227024 = $_POST['p227024'];
$p22703 = $_POST['p22703'];
$p221453 = $_POST['p221453'];
$p291003 = $_POST['p291003'];
$p2910024 = $_POST['p2910024'];
$p291453 = $_POST['p291453'];
$p441003 = $_POST['p441003'];
$p4410024 = $_POST['p4410024'];
$p441453 = $_POST['p441453'];
$b229808 = $_POST['b229808'];
$p2210012 = $_POST['p2210012'];
$p2210024 = $_POST['p2210024'];
$b229824 = $_POST['b229824'];
$b229820 = $_POST['b229820'];
$b229812 = $_POST['b229812'];
$b2214524 = $_POST['b2214524'];
// begin the transaction
$conn->beginTransaction();
// our SQL statememtns
$conn->exec("INSERT INTO Eprocessing (date, amount, product, sort, notes, type, optype)
VALUES
('$date', '$b229824', '22×98×2,4', '0-2', '$notes', '$type', 'Production'),
('$date', '$b229820', '22×98×2,0', '0-2', '$notes', '$type', 'Production'),
('$date', '$b229812', '22×98×1,2', '0-2', '$notes', '$type', 'Production'),
('$date', '$b2214524', '22×145×2,4', '0-2', '$notes', '$type', 'Production'),
('$date', '$p1910024', '19×100×2,4', '0-2', '$notes', '$type', 'Production'),
('$date', '$p291003', '29×100×3,0', '0-2', '$notes', '$type', 'Production'),
('$date', '$p2910024', '29×100×2,4', '0-2', '$notes', '$type', 'Production'),
('$date', '$p291453', '29×145×3,0', '0-2', '$notes', '$type', 'Production'),
('$date', '$p441003', '44×100×3,0', '0-2', '$notes', '$type', 'Production'),
('$date', '$p4410024', '44×100×2,4', '0-2', '$notes', '$type', 'Production'),
('$date', '$p441453', '44×145×3,0', '0-2', '$notes', '$type', 'Production'),
('$date', '$b229808', '22×98×0,8', '0-2', '$notes', '$type', 'Production'),
('$date', '$p2210012', '22×100×1,2', '0-2', '$notes', '$type', 'Production'),
('$date', '$p2210024', '22×100×2,4', '0-2', '$notes', '$type', 'Production'),
('$date', '$p227024', '22×70×2,4', '0-2', '$notes', '$type', 'Production'),
('$date', '$p22703', '22×70×3,0', '0-2', '$notes', '$type', 'Production'),
('$date', '$p221003', '22×100×3,0', '0-2', '$notes', '$type', 'Production')");
$conn->exec("DELETE FROM Eprocessing WHERE amount=0");
// commit the transaction
$conn->commit();
echo "Added to database";
include("views/add_processing.php");
}
catch(PDOException $e)
{
// roll back the transaction if something failed
$conn->rollback();
echo "Error: " . $e->getMessage();
} //disconnect
有没有办法从sql数据库加载变量列表?
对我的情况是否有更好的解决方案?
我希望我的问题清楚,如果没有,请告诉我。
提前谢谢。
答案 0 :(得分:0)
我不确定我是否理解正确,但如果您希望$_POST
数据中的每个项目都成为单独的变量,则可以使用extract。
extract($_POST, EXTR_SKIP);
EXTR_SKIP
确保不会覆盖任何现有变量。这是为了安全。
然后你有变量$p191003
,$p1910024
等。$_POST
array
键将是变量名。
答案 1 :(得分:0)
$array = array();
foreach($_POST as $key => $value){
$array[$key] = $value;
}
print_r($array); /* just to see it */
然后你可以遍历数组来运行你的查询