-2

As you can see in the picture I have multiple button inside one FORM so when I click on button It insert always the last value :: S (126).

This is my code:

<form method="post" action="" enctype="multipart/form-data">

  <input type="text" class="form-control" placeholder="Numéro de chassit" name="chassit" id="chassit" >

  <?php 
    if (mysqli_num_rows($result) > 0) 
  { ?>
  <?php 
    while($row = mysqli_fetch_array($result)) { 
  ?>

  <?php echo "<img alt='' src='../admin/image/mercedes/".$row['image']."' >";?>
  <div class="card-body">
     <input id="model" name="model" type="hidden" value="<?=$row['model'] ?>">
     <input type="submit" name="mo" class="btn btn-secondary" value="<?=$row['model'] ?>">
  </div>

</form>

And this is the process

<?php
  if(ISSET($_POST['mo'])){

  $demandeur = $_SESSION['username'];
  $model = $_POST['model'];
  $chassit = $_POST['chassit'];

    $sql = "INSERT INTO tbl_xp_support (demandeur,model,chassit)
    VALUES ('$demandeur','$model','$chassit')";

if (mysqli_query($con, $sql)) {
header("Location: ../xp_group.php");
} else {
echo "Error: " . $sql . " " . mysqli_error($con);
}
mysqli_close($con);
}
 ?>

The input name="model" it insert always the last value !!!

picture

walid
  • 51
  • 5
  • Why does this need a second question now? [How can I open submit button with dynamic name?](https://stackoverflow.com/questions/73151670/how-can-i-open-submit-button-with-dynamic-name) – CBroe Jul 29 '22 at 09:04
  • _"The input name="model" it insert always the last value !!!"_ - well then stop using that field altogether, and use the value of the button itself instead. (As you had already been told when you asked this question for the first time.) – CBroe Jul 29 '22 at 09:06
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Jul 29 '22 at 09:23

2 Answers2

1

Because your all input fields has same name, so last one will overwrite any previous ones. If you want to select only one that was selected, you should make it as radio button:

  <label class="card-body">
     <input id="model" name="model" type="radio" style="display: none;" value="<?=$row['model'] ?>"/>
     <input type="submit" name="mo" class="btn btn-secondary" value="<?=$row['model'] ?>">
  </label>

Please note:

  1. you never use $_POST['mo'] value that should contain selected model
  2. You have same ID in every iteration, and that is invalid HTML
Justinas
  • 41,402
  • 5
  • 66
  • 96
1

Your browser has no way to know that each submit button is supposed to be associated with a single hidden input, since they're all part of the same form. What will actually happen is that regardless of which button is pressed, all of the hidden fields will be submitted.

Since they all have the same name, but different values, PHP has to decide which value to actually put into the $_POST array. Its policy is to take the last value, which is what you see.

There are a few ways I can think of to make this work:

  • Put each hidden field and submit button into its own HTML <form>, rather than having one for the whole page.
  • Have a single hidden field at the bottom of the page, and write some JavaScript that updates that hidden field when you click a button, before submitting the form.
  • Look at the value of the submit button by examining the $_POST['mo'] variable, and get rid of the hidden fields completely. But beware that there are some extra cases you need to consider when doing this.
IMSoP
  • 89,526
  • 13
  • 117
  • 169