Chee Seng Teo Chee Seng Teo - 3 months ago 6
HTML Question

How to insert data once for two input has same name

Please have a look
Website interface

I only can insert these data with click both of the submit button.
I wish to use only one submit button to submit these data twice.

Here is my code.
As u can see, I for loop this form

<?php
for($x=1; $x<=2; $x++)
{
?>
<div class="col-lg-6">
<label>Passenger <?php echo $x ?> </label>
<form role="form" method='post'>
<div class="form-group">
<label>Title</label>
<label class="radio-inline">
<input type="radio" name="gander" value="Mr" checked>Mr
</label>
<label class="radio-inline">
<input type="radio" name="gander" value="Ms">Ms
</label>
</div>
<div class="form-group">
<label>Enter name of Passenger</label>
<input class="form-control" name='name'>
</div>
<div class="form-group">
<label>Enter Passenger Id <?php echo $x ?></label>
<input class="form-control" placeholder="960529-01-6925" name='ic'>
</div>
<button type="submit" class="btn btn-default" name="addbtn" method="post">Submit Button</button>
</form>
</div>

<?php
}
?>


Here is the isset button code

<?php
if(isset($_POST["addbtn"]))
{

$ganderphp = $_POST["gander"];
$namephp = $_POST["name"];
$icphp = $_POST["ic"];
$sql = "INSERT INTO `passenger_data`(`pass_gender`,`pass_name`,`pass_ic`,`Username`) VALUES ('$ganderphp','$namephp','$icphp','$username')";
if (mysqli_query($conn, $sql)) {
echo $sql;
}else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}

?>


Its not user friendly because user only can submit data on one of the form first.
I did try to take the submit button out of the looping, but its not working.
I need a solution to make user can submit only once for all these data.

Base on some research, maybe array[] and for each can done this but i just don know how to use it.

Appreciate for any answer.

Answer

First of all, take the <form> and submit <button> element outside of the for loop, and then change the name attributes like this:

name="gander[<?php echo $x; ?>]"
name='name[<?php echo $x; ?>]' 

and

name='ic[<?php echo $x; ?>]'

So your HTML form would be like this:

<div class="col-lg-6">
    <form role="form" method='post'>
<?php
    for($x=1; $x<=2; $x++){
?>
    <label>Passenger <?php echo $x ?> </label>
    <div class="form-group">
          <label>Title</label>
          <label class="radio-inline">
        <input type="radio" name="gander[<?php echo $x; ?>]" value="Mr" checked>Mr
          </label>
          <label class="radio-inline">
                 <input type="radio" name="gander[<?php echo $x; ?>]"  value="Ms">Ms
          </label>
      </div>
      <div class="form-group">
          <label>Enter name of Passenger</label>
          <input class="form-control" name='name[<?php echo $x; ?>]'>
      </div>
      <div class="form-group">
      <label>Enter Passenger Id <?php echo $x ?></label>
     <input class="form-control" placeholder="960529-01-6925" name='ic[<?php echo $x; ?>]'>
     </div>
    <?php
    }
    ?>
    <button type="submit" class="btn btn-default" name="addbtn" method="post">Submit Button</button>
    </form>
</div>

And after submitting, process your form like this:

if(isset($_POST["addbtn"])){
    for($x = 1; $x <= 2; ++$x){
        $ganderphp = $_POST["gander"][$x];
        $namephp = $_POST["name"][$x];
        $icphp = $_POST["ic"][$x];

        // construct the query and execute it

    }
}

Sidenote: Learn about prepared statements because right now your query is susceptible to SQL injection. Also see how you can prevent SQL injection in PHP.