简体   繁体   English

我如何在while标记中提交php中的所有字段

[英]How do I submit all fields within a php while tag

Im trying to post the generated form to a second file for further processing, however only the last entry is sent to the second file. 我试图将生成的表单发布到第二个文件进行进一步处理,但是只有最后一个条目发送到第二个文件。 How do I send all information in the loop to the second page? 如何将循环中的所有信息发送到第二页? Additionally the $_POST['STAMP'] entry is not being posted. 此外,不会发布$_POST['STAMP']条目。 Do I need a different approach to retrieve the DB data? 我是否需要其他方法来检索数据库数据?

<!DOCTYPE html>
<html>
<body>

<form action="payc.php" method="post">
Pay Range 
  <input type="date" name="start">
<input type="date" name="stop">
  <input type="submit">
</form>

</body>
</html>

<?php
$servername = "localhost";
$username = "user";
$password = "Pass";
$dbname = "DB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM TIME WHERE STAMP BETWEEN '". $_POST['start']."' AND '".$_POST['stop']."'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

echo "<form action=\"test.php\" method=\"post\">";
    while($row = $result->fetch_assoc()){
$duration=(($row['T_OUT']-$row['T_IN'])/60)/60;


        echo "<fieldset><input type=\"hidden\" name=\"date\" value=\"".$_POST['STAMP']."\"><input name=\"EMP\" type=\"text\" value=\"". $row['EMP']. "\"><input name=\"time\" type=\"text\" value=\"" . $duration. "\"><input name=\"ITEM\" type=\"text\" value=\"" . $row['ITEM']. "\"><input name=\"NOTE\" type=\"text\" value=\"" . $row['NOTE']. "\"></fieldset>";

        }
echo "<input type=\"submit\">";
echo "</form>";
} else {
    echo "0 results";
}
foreach($_POST as $results){
var_dump($results);
}

$conn->close();
?>

The problem is that all the fields have the same name at every loop 问题在于,每个字段在每个循环中都具有相同的名称

The solution consists of two parts: 该解决方案包括两部分:

<input name="date[]">
<input name="EMP[]">
<input name="time[]">
<input name="ITEM[]">
<input name="NOTE[]">

Which will identify it as array not one input 哪个将其标识为数组而不是一个输入

Second part at the test.php file Instead of getting one input with $_POST['date'] It will return an array so you have to loop over them test.php文件的第二部分,而不是使用$ _POST ['date']获得一个输入,它将返回一个数组,因此您必须遍历它们

$info = array();
for ( $index = 0 ; $index < count($_POST['date']) ; $index++ )
{
    $record = array();
    $record['date'] = $_POST['date'][$index];
    $record['EMP'] = $_POST['EMP'][$index];
    $record['time'] = $_POST['time'][$index];
    $record['ITEM'] = $_POST['ITEM'][$index];
    $record['NOTE'] = $_POST['NOTE'][$index];
    $info[] = $record
}

1) You are vulnerable to sql injection attacks 1)您容易受到sql注入攻击

2) You never check if the form was submitted at all - your php code runs regardless of HOW the page was loaded. 2)您永远不会检查表单是否已提交-无论页面如何加载,您的php代码都会运行。 At minimum, you should have something like 至少,您应该有类似

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
   ... db stuf ...
}

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

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