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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Zeta Theo
  • 1
  • 1
  • 1
    Please **don't use `mysql_*` functions in new code** ([why?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)), they are [deprecated](https://wiki.php.net/rfc/mysql_deprecation). Use [PDO or MySQLi](http://php.net/manual/en/mysqlinfo.api.choosing.php) instead – kero Mar 26 '13 at 21:51

4 Answers4

0

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>";
Nelson
  • 49,283
  • 8
  • 68
  • 81
0

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

kero
  • 10,647
  • 5
  • 41
  • 51
0

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

Michael
  • 11,912
  • 6
  • 49
  • 64
0

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']);
}
kero
  • 10,647
  • 5
  • 41
  • 51
thaJeztah
  • 27,738
  • 9
  • 73
  • 92