我的循环有什么问题

时间:2016-11-14 19:08:07

标签: php database postgresql loops

我的循环有问题。我要插入的数据只有2个名称,但数据库中插入的数据每个名称为4,这意味着它将是8个名称。是我的编码

- 编码index.php

<html>  
  <head>  
       <title>E- Aqiqah</title>  
       <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
       <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
  </head>  
  <body>  
       <div class="container">  
            <br />  
            <br />  
            <h2 align="center">E-Aqiqah</h2>  
            <div class="form-group">  
                 <form name="add_name" id="add_name">  
                      <div class="table-responsive">  
                           <table class="table table-bordered" id="dynamic_field">  
                                <tr>  
                                    <td align="right"><b>Nama : </b></td>
                                     <td><input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></td>  
                                     <td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>  
                                </tr>  
                                <tr>
                                <td align="right"><b>Jantina : </b></td>
                                <td><select name="gender[]" id="gender">
                  <option selected="selected" value="">--Pilih--</option>
                  <option value="PEREMPUAN">PEREMPUAN</option>
                  <option value="LELAKI">LELAKI</option>
                </select></td> 
                </tr>
                <tr>
                <td align="right"><b>Umur : </b></td>
                <td><input type="text" name="age[]"  class="form-control name_list" /></td> 
                </tr>
                           </table>  
                           <input type="button" name="submit" id="submit" class="btn btn-info" value="Submit" />  
                      </div>  
                 </form>  
            </div>  
       </div>  
  </body>  
 </html>  
 <script>  
 $(document).ready(function(){  
   var i=1; 
    var j=1;
    var k=1;
  $('#add').click(function(){  
       i++;  
       k++;
       j++;
       $('#dynamic_field').append('<tr id="row'+i+'"><td align="right"><b>Nama : </b></td><td><input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></td><td><button type="button" name="remove" id="'+i+','+j+','+k+'" class="btn btn-danger btn_remove">X</button></td></tr>');
        $('#dynamic_field').append('<tr id="row'+j+'"><td align="right"><b>Jantina : </b></td><td><select name="gender[]" id="gender"> <option selected="selected" value="">--Pilih--</option><option value="PEREMPUAN">PEREMPUAN</option><option value="LELAKI">LELAKI</option></select></td></tr>');         
        $('#dynamic_field').append('<tr id="row'+k+'"><td align="right"><b>Umur : </b></td><td><input type="text" name="age[]"  class="form-control name_list" /></td></tr>');
  });  
  $(document).on('click', '.btn_remove', function(){  
       var button_id = $(this).attr("id");   
       $('#row'+button_id+'').remove();  
  });  
  $('#submit').click(function(){            
       $.ajax({  
            url:"name.php",  
            method:"POST",  
            data:$('#add_name').serialize(),  
            success:function(data)  
            {  
                 alert(data);  
                 $('#add_name')[0].reset();  
            }  
       });  
  });  
 });  
 </script>  

- 编码name.php

  <?php  
 $connect = pg_connect ("user = postgres password = syafiqah26 port = 5433   dbname = bengkel2 host = localhost");
 $number = count($_POST["name"]);  
 $number1 = count($_POST["gender"]);
 $number2 = count($_POST["age"]);

 if(($number > 0)&&($number1>0)&&($number2>0) )  
 {  
  for($i=0; $i<$number; $i++)  
  {  
    for($j=0; $j<$number1; $j++)
    {
    for($k=0; $k<$number2; $k++)
    {
       if((trim($_POST["name"][$i] != ''))&&(trim($_POST["gender"][$j] !=  ''))&&(trim($_POST["age"][$k] != '')))  
        {  
            $sql = "INSERT INTO trynominee(name,gender,age)  VALUES('".pg_escape_string($connect, $_POST["name"] [$i])."','".pg_escape_string($connect, $_POST["gender"] [$j])."','".pg_escape_string($connect, $_POST["age"][$k])."')";  
            pg_query($connect, $sql);  

       }  
     }  
     echo "Data Inserted";  

 }  
  }
 }
 else  
 {  
  echo "Please Enter Name";  
 } 
   pg_close($connect); 
 ?>  

Tq帮助我

0 个答案:

没有答案