gpt4 book ai didi

php - 如何从 sql 表向 php 表单处理程序添加变量?

转载 作者:行者123 更新时间:2023-11-30 22:34:05 26 4
gpt4 key购买 nike

大家好!

当我们将 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'];

编辑1:

这是我将规范添加到数据库的方式:

<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&#215;$w&#215;$l', '$v', '$type', 'sp$height$width$lvar')");
}

if ($type == 'bir') {
$conn->exec("INSERT INTO Rspecs (name, volume, type, scode) VALUES ('$h&#215;$w&#215;$l', '$v', '$type', 'bir$height$width$lvar')");
}

if ($type == 'asp') {
$conn->exec("INSERT INTO Rspecs (name, volume, type, scode) VALUES ('$h&#215;$w&#215;$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&#215;98&#215;2,4', '0-2', '$notes', '$type', 'Production'),
('$date', '$b229820', '22&#215;98&#215;2,0', '0-2', '$notes', '$type', 'Production'),
('$date', '$b229812', '22&#215;98&#215;1,2', '0-2', '$notes', '$type', 'Production'),
('$date', '$b2214524', '22&#215;145&#215;2,4', '0-2', '$notes', '$type', 'Production'),
('$date', '$p1910024', '19&#215;100&#215;2,4', '0-2', '$notes', '$type', 'Production'),
('$date', '$p291003', '29&#215;100&#215;3,0', '0-2', '$notes', '$type', 'Production'),
('$date', '$p2910024', '29&#215;100&#215;2,4', '0-2', '$notes', '$type', 'Production'),
('$date', '$p291453', '29&#215;145&#215;3,0', '0-2', '$notes', '$type', 'Production'),
('$date', '$p441003', '44&#215;100&#215;3,0', '0-2', '$notes', '$type', 'Production'),
('$date', '$p4410024', '44&#215;100&#215;2,4', '0-2', '$notes', '$type', 'Production'),
('$date', '$p441453', '44&#215;145&#215;3,0', '0-2', '$notes', '$type', 'Production'),
('$date', '$b229808', '22&#215;98&#215;0,8', '0-2', '$notes', '$type', 'Production'),
('$date', '$p2210012', '22&#215;100&#215;1,2', '0-2', '$notes', '$type', 'Production'),
('$date', '$p2210024', '22&#215;100&#215;2,4', '0-2', '$notes', '$type', 'Production'),
('$date', '$p227024', '22&#215;70&#215;2,4', '0-2', '$notes', '$type', 'Production'),
('$date', '$p22703', '22&#215;70&#215;3,0', '0-2', '$notes', '$type', 'Production'),
('$date', '$p221003', '22&#215;100&#215;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数据库加载变量列表?

对于我的情况,是否有更好的解决方案?

我希望我说清楚了我的问题,如果没有请告诉我。

提前谢谢你。

最佳答案

不确定我是否理解正确,但如果您希望将 $_POST 数据中的每个项目放入单独的变量中,您可以使用 extract .

extract($_POST, EXTR_SKIP);

EXTR_SKIP 确保任何现有变量都不会被覆盖。这是为了安全。

然后你有变量 $p191003, $p1910024 等。 $_POST array 键将是变量名.

关于php - 如何从 sql 表向 php 表单处理程序添加变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33037164/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com