简体   繁体   中英

Only getting the last row from a table using POST with PHP

Ι'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.

You forgot to correctly end your <input> tags, like this:

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

Your code has multiple flaws. As Was stated, you forgot to close the tags.

Also you open a <form> in the inner while, however you only close it once (after the submit button).

Another problem is that you have various fields with the same name: percentage , this can be solved by replacing name="percentage" with name="percentage[]" . If you access it in PHP, you will have an array with the percentages and not just one field

Firstly, you're skipping the first row here:

$row = mysql_fetch_array($result);

// ...

while($row = mysql_fetch_array($result))

Remove the $row = ... that you have immediately beneath the query.

Secondly, you're iterating over the rows whilst iterating over the rows which is just bizarre. Change your while loop to (and close the <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>";
     }

 }

Finally, open the <form> before you open the <table> .

You need to use a special 'array' notation for your form-inputs, so that the same field can be included multiple times on your page;

Change percentage to percentage[] ; also you're missing the closing tag of the input tags in your code

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

Then, to display the values;

if (!empty($_POST['percentage'])) {
    print_r($POST['percentage']);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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