0

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.

rubinmon
  • 153
  • 3
  • 12

1 Answers1

1

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.

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • Its working! Appreciate your answer so quickly ! Thanks. – Chee Seng Teo Sep 08 '16 at 17:07
  • Since, u are note about sql injection, I actually had a big question about this. I am using DB_function to prevent this, but i never success to sql injection anything by `'); DROP TABLE table;--` so if possible, would u help me to have a look about mine DB_function is working to prevent sql injection? – Chee Seng Teo Sep 08 '16 at 17:27
  • In DB_function, i basically write all sql in another page, its really different with stmt. Here is the code in DB_function if i want to add data `public function add($tablename,$colname,$dataname){ $sql = "INSERT INTO ".$tablename." (".$colname.") VALUES (".$dataname.")"; $result= mysqli_query(mysqli_connect("localhost","root","","phpstmt"), $sql); return $result; }` – Chee Seng Teo Sep 08 '16 at 17:30
  • So in Interface i only need to write like this if i want to add data `dataphp = $_POST["data"]; $datatwophp = $_POST["datatwo"]; $colname = "data,datatwo"; $dataname = "'$dataphp','$datatwophp'"; $result = $db->add($tablename,$colname,$dataname);` – Chee Seng Teo Sep 08 '16 at 17:31
  • I am sorry, i cant make the code look better in comment. – Chee Seng Teo Sep 08 '16 at 17:35
  • @CheeSengTeo No, that's not correct. Always *prepare*, *bind* and *execute* your queries, because that's how you can prevent SQL injection. – Rajdeep Paul Sep 08 '16 at 17:36
  • @CheeSengTeo *Cheers! :-)* – Rajdeep Paul Sep 08 '16 at 17:41