繁体   English   中英

如何从SQL表向PHP表单处理程序添加变量?

[英]How to add variables to a php form handler from sql table?

大家好!

当我们将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&#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键将是变量名。

你可以做这样的事情...

$array = array();

foreach($_POST as $key => $value){
    $array[$key] = $value;
}

print_r($array); /* just to see it */

然后,您可以遍历数组以运行查询

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM