0

Ι'm new in PHP and I have a problem in getting data from POST. I have an SQL query which selects from a table 3 columns, which I echo in a table form. Notice that only the 3rd column should be input type. Although the table is displayed fine (all values from mySQL), when I print the 3rd column variable from $_POST, only the last value is printed. Here is the code:

$result = mysql_query("SELECT `id`, `component`,`percentage` FROM `waste_percentage` ")or die(mysql_error());
$row = mysql_fetch_array($result);

echo "<table border='1' cellspacing='0'>
<tr>
<th>α/α</th>
<th>Ρεύμα</th>
<th>Ποσοστό</th>
</tr>"; 
while($row = mysql_fetch_array($result))
     {
   $num = mysql_numrows($result);
   $i=0;
       while ($i < $num) 
         {
          $field1 = $i +1;
          $field2=mysql_result($result,$i,"component");
          $field3=mysql_result($result,$i,"percentage");
          $i++;
          echo "<form action=\"\" method=\"post\">";
          echo "<tr>";
          echo "<td> $field1 </td>";
          echo "<td> $field2 </td>";
          echo "<td>" . "<input type=\"text\" name=\"percentage\" value=" .  $field3 . " </td>";
          echo "<td>" . "<input type=\"hidden\" name=\"hidden\" value=" . $row['id'] . " </td>";
          echo "</tr>";
         }

     }
 echo "</table>";
 echo "<input name=\"Submitpercent\" type=\"submit\" value=\"Συνέχεια\" />";
 echo "</form>";

  //When I try to output 

  if(isset($_POST['Submitpercent'])) 
  {
   $user_percentage [] = $_POST['percentage'];
   print_r ($user_percentage);
  }

Output is: 'Array ( [0] => 13.60 )'. I would appreciate any help! Thanks in advance.

4

4 回答 4

0

首先,您在这里跳过第一行:

$row = mysql_fetch_array($result);

// ...

while($row = mysql_fetch_array($result))

删除$row = ...您在查询下方的那个。

其次,您在遍历行的同时遍历行,这很奇怪。将循环更改while为(并关闭<inputs>):

$i=0;

while($row = mysql_fetch_array($result))
{

      $field1 = ++$i;
      $field2 = $row['component'];
      $field3 = $row['percentage'];

      echo "<tr>";
      echo "<td> $field1 </td>";
      echo "<td> $field2 </td>";
      echo "<td>" . "<input type=\"text\" name=\"percentage\" value=\"" .  $field3 . "\"> </td>";
      echo "<td>" . "<input type=\"hidden\" name=\"hidden\" value=\"" . $row['id'] . "\"> </td>";
      echo "</tr>";
     }

 }

最后,<form>在打开<table>.

于 2013-03-26T21:59:05.413 回答
0

您需要为表单输入使用特殊的“数组”表示法,以便同一字段可以多次包含在您的页面上;

更改percentagepercentage[]; 您还缺少代码中输入标签的结束标签

echo '<td><input type="text" name="percentage[]" value="' .  $field3 . '" /></td>";

然后,显示值;

if (!empty($_POST['percentage'])) {
    print_r($POST['percentage']);
}
于 2013-03-26T21:59:12.517 回答
0

您忘记正确结束<input>标签,如下所示:

echo "<td>" . "<input type=\"text\" name=\"percentage\" value=\"" .  $field3 . "\"> </td>";
echo "<td>" . "<input type=\"hidden\" name=\"hidden\" value=\"" . $row['id'] . "\"> </td>";
于 2013-03-26T21:53:40.197 回答
0

您的代码有多个缺陷。如前所述,您忘记关闭标签。

你也可以在内部打开一个<form>,但是你只关闭一次(在提交按钮之后)。

另一个问题是您有多个具有相同名称的字段:percentage,这可以通过替换name="percentage"为来解决name="percentage[]"。如果您在 PHP 中访问它,您将拥有一个包含百分比的数组,而不仅仅是一个字段

于 2013-03-26T21:56:48.493 回答