What are the potential pitfalls of using the same name for input fields in a PHP form?

Using the same name for input fields in a PHP form can cause issues when trying to access the form data using $_POST or $_GET as it will only return the value of the last input field with that name. To solve this issue, you can append square brackets [] to the input field name to make it an array, allowing you to access all values.

<form method="post">
  <input type="text" name="input_field[]">
  <input type="text" name="input_field[]">
  <input type="text" name="input_field[]">
  <input type="submit" value="Submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $input_values = $_POST['input_field'];
  
  foreach ($input_values as $value) {
    echo $value . "<br>";
  }
}
?>