简体   繁体   English

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

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

Hello everybody! 大家好!

When we insert html form data into an sql table, we have this variable list in the handler document: 当我们将html表单数据插入sql表时,处理程序文档中将包含以下变量列表:

$date = $_POST['date'];
$amount = $_POST['amount'];
$number = $_POST['number'];

And so on. 等等。

Now - I have a sawmill, where lumber with different specifications is being produced. 现在-我有一个锯木厂,正在生产各种规格的木材。 Those specifications are stored in an sql table. 这些规范存储在sql表中。 I can add new ones by using a html form (for example pine boards 19mm*100mm*3m will create a 'p191003' entry in the table). 我可以使用html表单添加新的表单(例如,19mm * 100mm * 3m的松木板将在表中创建一个“ p191003”条目)。 This is then used as an input field name in another form, where I have a table with all the sizes and input fields to enter the amounts (number of logs produced). 然后将其用作另一种形式的输入字段名称 ,在这里我有一张表,其中包含所有大小和输入字段,用于输入金额(生成的日志数量)。 Every day the produced amounts are submitted to another sql table. 每天将产生的金额提交到另一个sql表。 My problem is, I need every added size to 'appear' in the variable list to be able to submit the amount produced to the database. 我的问题是,我需要将每个增加的大小“显示”在变量列表中,以便能够将产生的数量提交到数据库。 If I did it manually, it would look like this: 如果我手动执行此操作,则它将如下所示:

$p191003 = $_POST['p191003'];
$p1910024 = $_POST['p1910024'];
$p221003 = $_POST['p221003'];
$p227024 = $_POST['p227024'];
$p22703 = $_POST['p22703'];
$p221453 = $_POST['p221453'];
$p291003 = $_POST['p291003'];

EDIT1: EDIT1:

This is how I add the specifications to the database: 这是我将规范添加到数据库的方式:

<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>

This is the handler: 这是处理程序:

//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

This is the form to add the produced sizes (php part): 这是添加生产的尺寸(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

And this is the handler I used before (when I had to enter the sizes manually) 这是我以前使用的处理程序(当我必须手动输入尺寸时)

$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

Is there a way to load the variables list from the sql database? 有没有办法从sql数据库加载变量列表?

Is there perhaps a better solution to my situation? 是否可以针对我的情况提供更好的解决方案?

I hope I made my problem clear, please tell me if not. 希望我已明确我的问题,如果没有,请告诉我。

Thank you in advance. 先感谢您。

Not sure I understand you correctly, but if you want every item in $_POST data into separate variable, you can use extract . 不确定我是否正确理解您的意思,但是如果您希望$_POST数据中的每个项目都成为单独的变量,则可以使用extract

extract($_POST, EXTR_SKIP);

EXTR_SKIP makes sure that any of existing variables will not be overwritten. EXTR_SKIP确保任何现有变量都不会被覆盖。 This is for security. 这是出于安全考虑。

Then you have variables $p191003 , $p1910024 etc. $_POST array keys will be variable names. 然后,您将获得变量$p191003$p1910024等。 $_POST array键将是变量名。

you could do something like this... 你可以做这样的事情...

$array = array();

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

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

Then you could loop through the array to run your queries 然后,您可以遍历数组以运行查询

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

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