0

So basically what I have is a simple table inside a form. The goal is to be able to add amount on top of the current amount. At the moment I have a table in my database which consists of 2 Foreign Keys. In this case, the locationcode, and productcode (don't mind the Dutch translation):

if(isset($_POST['submit'])){
    // These both give me a undefined index error.
    $locatiecode = $_POST["locatiecode"];
    $productcode = $_POST["productcode"];

    $input = $_POST["input"];

    //I suppose the SQL query here below should work once I have fixed the other issues?
$sql2 = "UPDATE aantal SET aantal = aantal + '$input' WHERE locatiecode = $locatiecode AND productcode = productcode";
    $result2 = $con->query($sql2);
}

Thats the submit part. Below I have the while loop which selects data from the table, yet again don't mind the Dutch:

<form method="post" action="">
    <div style="width: 50%; margin: 0 auto; text-align: center;">
        <table class="table">
            <thead>
                <tr> 
                    <th>Locatiecode</th>
                    <th>Locatie</th>
                    <th>Productcode</th>
                    <th>Product</th>
                    <th>Fabriek</th>
                    <th>Inkoopprijs</th>
                    <th>Verkoopprijs</th>
                    <th>Aantal</th>

                </tr>
            </thead>
            <tbody>
                <?php

                $sql = "SELECT * FROM locatie INNER JOIN voorraad ON locatie.locatiecode = voorraad.locatiecode INNER JOIN product ON product.productcode = voorraad.productcode";
                $result = $con->query($sql);

                while($row = $result->fetch_assoc()) {
                    echo "<tr>";
                    echo "<th>" . $row["locatiecode"] . "</th>";
                    echo "<th>" . $row["locatie"] . "</th>";
                    echo "<th>" . $row["productcode"] . "</th>";
                    echo "<th>" . $row["productnaam"] . "</th>";
                    echo "<th>" . $row["fabrieknaam"] . "</th>";
                    echo "<th>" . $row["inkoopprijs"] . "</th>";
                    echo "<th>" . $row["verkoopprijs"] . "</th>";
                    echo "<th>" . $row["aantal"] . "</th>";
                    echo "<th><input type='text' name='input' id='aantalinvoer'></th>";
                    echo "<th><input type='submit' name='submit' id='submit'></th>";
                    echo "</tr>";
                }

                ?>
            </tbody>
        </table>
    </div>
</form>

So at the moment what happens is, I get a row, with each row an update button (submit button) and an input field. For as now, the input field named, input, is not able to POST. The location and productcode gives me an undefined index when I try to submit.

Does this have something to do with the while loop?

Help would be much appreciated!

Yakushi
  • 23
  • 1
  • 1
  • 5
  • your never setting `name="locatiecode"` in the code – Derek Apr 06 '17 at 18:06
  • You have a typo in your query of the 1st code block, you omitted the $ here `productcode = $productcode` – gmc Apr 06 '17 at 18:08
  • 2
    Your code is vulnerable to SQL injection attacks. You should use [mysqli](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) or [PDO](http://php.net/manual/en/pdo.prepared-statements.php) prepared statements as described in [this post](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky Apr 06 '17 at 18:08
  • Maybe this answer helps also: http://stackoverflow.com/questions/7880619/multiple-inputs-with-same-name-through-post-in-php#7880656 – giftnuss Apr 06 '17 at 18:11
  • Only form fields will be submitted when you press the submit button. Also, since you only have one form, the submit button will act on all input fields with that form, regardless of what table row it is in. You will also have an issue with having multiple fields with the name of "input". You generally should keep your id and name fields the same. However, since you have the input field repeated in a loop, you should look at using an array such as name="aantalinvoer[$key]" where $key is the id of the row you are on (possibly locatiecode?) – kojow7 Apr 06 '17 at 18:19

2 Answers2

0

$_POST["field"] references a corresponding input field in your form. In your example you don't have an input field with name="locatiecode"

In order to have the productcode and locatiecode values accessable through post, you should change the HTML to look something like

echo "<th>
        {$row['productcode']}
        <input type=\"hidden\" name=\"productcode\" value=\"{$row['productcode']}\" />
    </th>";
domwrap
  • 443
  • 1
  • 4
  • 12
0

And didn't understand exactly what you want to do.

  • If you want to submit only the values of the specific row where the submit was clicked you have to add a form for each loop, or you can do this with AJAX (the best way in my opinion).
  • If you want to send all the input values in one click, I recommend you to remove your submits from the loop and add one submit in the top or in the bottom of the table, so it can be clear to the user what exactly he is doing. Also you have put the input names as said in the post giftnuss mentioned, and the POST will send an array of values.

In any of this scenarios you have to add hidden inputs with the values of locatiecode and productcode.

And as mentioned, your SQL code is very insecure.

nankran
  • 26
  • 4