mysql - Get data from form using PHP


I have a php file that creates a table for me using data fetched from a mysql database.


if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
      echo "<tr>";
      echo "<td>" . $row['ID'] . "</td>";
      echo "<td>" . $row['Name'] . "</td>";
      echo "<td>" . $row['CountryCode'] . "</td>";
      echo "<td>" . $row['District'] . "</td>";
      echo "<td>" . $row['Population'] . "</td>";
      echo "<td>" . $row['Price'] . "</td>";
      echo "<td><input type=\"number\" name="quantity" id="quantity"></td>";
      echo "</tr>";
} else {
    echo "0 results";


At the end of each row, I have an input number so that I can calculate the amount of each product(countries in this case). How could I get the amount of each country, multiply it by the price and return a total at the end of my table?




If you want to do it with PHP, you have to do it in two steps, because the PHP is processed in the server. So you would enter all the quantities you want and when you press the "Compute" button, the page would send all the values to the server who would compute everything you want and give you the final result.

A problem that I see with this solution is that once you have get all the quantities you want, you would have to get the prices once more. I don't its very efficient because you already have them on the first page. I see two solutions here :

  1. When you display the values, keep track of the prices in a$_SESSION field, thus you will be able to access them after leaving the page, without having to get them from the database.
  2. When you display the values, display the prices in aninput field (I would recommend asreadonly so that the user won't change this value here), and thus they would be included in the$_POST (or$_GET, if you use this) variable that your browser would send to the server.

If you're only interested to display the value, without using it after, you can use javascript to compute it, it might be easier.

Even though it's not the point here, you should be careful when displayinginputs via PHP on a while loop, because here all of yourinputs have the sameid, which would make thing harder to compute what you want to.

Hope it helps!




You must add a form tag around the table and then sum record price * qty on every row

What I did here is I checked if the quantity is set in the POST variable or not. if it exists I put data in quantity value. after that, you must make the name of quantity capable of taking array inside with the key of record Id.

after that, if the form submitted the quantity will send back to the page as a query string. then I multiply the price with quantity and add it to sum varaible. after finishing the loop and echo out the sum variable at the end of the table.

$html = '';
if ($result->num_rows > 0) {
    $html .= '<form method="GET" action="" target="_self>';

    $row = $result->fetch_assoc();

        $qty = $_GET['quantity'][$row['ID']];
        $qty = 0;

    $sum = 0;
    // output data of each row
    while ($row) {
        $html .= '<tr>';
        $html .= '<td>' . $row['ID'] . '</td>';
        $html .= '<td>' . $row['Name'] . '</td>';
        $html .= '<td>' . $row['CountryCode'] . '</td>';
        $html .= '<td>' . $row['District'] . '</td>';
        $html .= '<td>' . $row['Population'] . '</td>';
        $html .= '<td>' . $row['Price'] . '</td>';
        $html .= '<td>
                    <input type="number" name="quantity[' . $row['ID'] . ']"  value="' . $qty . '">
        $html .= '<td>' . ($sum += $row['Price'] * $qty) . '</td>';
        $html .= '</tr>';
    $html .= '<input name="submitForm" value="submitForm">';
    $html .= '</form>';
    $html .= '<tr>';
    $html .= '<td>Total = '.$sum.'</td>';
    $html .= '</tr>';
} else {
    $html .= "0 results";

echo htmlspecialchars($html);

remember to use htmlspecialchars to echo out the result.

People are also looking for solutions to the problem: Issue pushing item to the end of a PHP array?


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.