0

I trying to insert multiple names with their corresponding surnames in mysql using php but it seems like there is something am doing wrong . I tried google and this site but i have not yet got the best answer to suit my requirements .I also managed to insert them but the surnames could go to a wrong username then I decided to delete that code and start a new one .Any help will be highly appreciated .below is what I tried .

 <form action ="dependant_Process_Trevor.php?memberid=<?php echo $_GET['memberid']; ?>" name="contactform"  method="post" id ="---contactform" >
               <input type="hidden" name ="memberid" value="<?php echo $_GET['memberid']; ?>" >
                <div class="box-header">
                </div>
            <!-- Start Trevor -->
              <div class="box box-danger">
                <div class="box-header with-border">
                  <h3 class="box-title">Dependents / Children under the age of 18 years - maximum 6 children</h3>
                </div>
                    <!-- First child -->
                <div class="box-body">
                  <div class="row">
                  <div class="col-xs-3">
                      <input type="text" name ="name[]"class="form-control" placeholder="Name">
                    </div>
                    <div class="col-xs-4">
                      <input type="text" name ="surname[]" class="form-control" placeholder="Surname">
                    </div>
                    <div class="col-xs-5">
                    <label>parent_type</label>
                   <select  name ="parent_type[]" "> 
                       <option  value ="" >Select Type</option>
                      <option name ="parent_type[]" value ="Son">Son</option>
                      <option name ="parent_type[]" value ="Daughter">Daughter</option>
                      <option name ="parent_type[]" value ="Brother">Brother</option>
                      <option name ="parent_type[]" value ="Grandchild">Grandchild</option>
                      <option name ="parent_type[]" value ="Nephew">Nephew</option>
                      <option name ="parent_type[]" value ="Niece">Niece</option>

                    </select>
                    </div>
                  </div>
                </div><!-- /.box-body -->
                <!-- End second  child -->

                    <!-- Start second child -->
                <div class="box-body">
                  <div class="row">
                   <div class="col-xs-3">
                      <input type="text" name ="name[]"class="form-control" placeholder="Name">
                    </div>
                    <div class="col-xs-4">
                      <input type="text" name ="surname[]" class="form-control" placeholder="Surname">
                    </div>
                    <div class="col-xs-5">
                    <label>parent_type</label>
                   <select  name ="parent_type[]" "> 
                       <option  value ="" >Select Type</option>
                     <option name ="parent_type[]" value ="Son">Son</option>
                      <option name ="parent_type[]" value ="Daughter">Daughter</option>
                      <option name ="parent_type[]" value ="Brother">Brother</option>
                      <option name ="parent_type[]" value ="Grandchild">Grandchild</option>
                      <option name ="parent_type[]" value ="Nephew">Nephew</option>
                      <option name ="parent_type[]" value ="Niece">Niece</option>

                    </select>
                    </div>
                  </div>
                </div><!-- /.box-body -->
                <!-- End second  child -->
                    <!-- Third  child -->
                <div class="box-body">
                  <div class="row">
                   <div class="col-xs-3">
                      <input type="text" name ="name[]"class="form-control" placeholder="Name">
                    </div>
                    <div class="col-xs-4">
                      <input type="text" name ="surname[]" class="form-control" placeholder="Surname">
                    </div>
                    <div class="col-xs-5">
                    <label>parent_type</label>
                   <select  name ="parent_type[]" "> 
                       <option  value ="" >Select Type</option>
                     <option name ="parent_type[]" value ="Son">Son</option>
                      <option name ="parent_type[]" value ="Daughter">Daughter</option>
                      <option name ="parent_type[]" value ="Brother">Brother</option>
                      <option name ="parent_type[]" value ="Grandchild">Grandchild</option>
                      <option name ="parent_type[]" value ="Nephew">Nephew</option>
                      <option name ="parent_type[]" value ="Niece">Niece</option>

                    </select>
                    </div>
                  </div>
                </div><!-- /.box-body -->
                <!-- End third   child -->

                <!-- Fourth child -->
                <div class="box-body">
                  <div class="row">
                  <div class="col-xs-3">
                      <input type="text" name ="name[]"class="form-control" placeholder="Name">
                    </div>
                    <div class="col-xs-4">
                      <input type="text" name ="surname[]" class="form-control" placeholder="Surname">
                    </div>
                    <div class="col-xs-5">
                    <label>parent_type</label>
                   <select  name ="parent_type[]" "> 
                       <option  value ="" >Select Type</option>
                       <option name ="parent_type[]" value ="Son">Son</option>
                      <option name ="parent_type[]" value ="Daughter">Daughter</option>
                      <option name ="parent_type[]" value ="Brother">Brother</option>
                      <option name ="parent_type[]" value ="Grandchild">Grandchild</option>
                      <option name ="parent_type[]" value ="Nephew">Nephew</option>
                      <option name ="parent_type[]" value ="Niece">Niece</option>

                    </select>
                    </div>
                  </div>
                </div><!-- /.box-body -->
                <!-- End second  child -->

                    <!-- Fifth child -->
                <div class="box-body">
                  <div class="row">
                   <div class="col-xs-3">
                      <input type="text" name ="name[]"class="form-control" placeholder="Name">
                    </div>
                    <div class="col-xs-4">
                      <input type="text" name ="surname[]" class="form-control" placeholder="Surname">
                    </div>
                    <div class="col-xs-5">
                    <label>parent_type</label>
                   <select  name ="parent_type[]" "> 
                       <option  value ="" >Select Type</option>
                       <option name ="parent_type[]" value ="Son">Son</option>
                      <option name ="parent_type[]" value ="Daughter">Daughter</option>
                      <option name ="parent_type[]" value ="Brother">Brother</option>
                      <option name ="parent_type[]" value ="Grandchild">Grandchild</option>
                      <option name ="parent_type[]" value ="Nephew">Nephew</option>
                      <option name ="parent_type[]" value ="Niece">Niece</option>
                    </select>
                    </div>
                  </div>
                </div><!-- /.box-body -->
                <!-- End fifth  child -->
                    <!-- six  child -->
                <div class="box-body">
                  <div class="row">
                    <div class="col-xs-3">
                      <input type="text" name ="name[]"class="form-control" placeholder="Name">
                    </div>
                    <div class="col-xs-4">
                      <input type="text" name ="surname[]" class="form-control" placeholder="Surname">
                    </div>
                    <div class="col-xs-5">
                    <label>parent_type</label>
                   <select  name ="parent_type[]" "> 
                       <option  value ="" >Select Type</option>
                      <option name ="parent_type[]" value ="Son">Son</option>
                      <option name ="parent_type[]" value ="Daughter">Daughter</option>
                      <option name ="parent_type[]" value ="Brother">Brother</option>
                      <option name ="parent_type[]" value ="Grandchild">Grandchild</option>
                      <option name ="parent_type[]" value ="Nephew">Nephew</option>
                      <option name ="parent_type[]" value ="Niece">Niece</option>

                    </select>
                    </div>
                  </div>
                </div><!-- /.box-body -->
                <!-- End six  child -->

              </div><!-- /.box -->
              <!-- End  Trevor -->  

                <div class="box-body">
                  <!-- Date dd/mm/yyyy -->
                  <div class="form-group">
                    <div class="input-group">
                      <div class="input-group-addon">
                      </div>
                    </div><!-- /.input group -->
                  </div><!-- /.form group -->

<input type="submit" name ="submit" value ="Save Data" class="btn-primary">
</form>

below is my php

//$surname =(isset($_POST['surname']))? trim ($_POST['surname']):'';
$recieved_by =($_SESSION['username']);
$othername =(isset($_POST['othername']))? trim ($_POST['othername']):'';
$gender =(isset($_POST['gender']))? trim ($_POST['gender']):'';
//$parent_type =(isset($_POST['parent_type']))? trim ($_POST['parent_type']):'';
$id_passport =(isset($_POST['id_passport']))? trim ($_POST['id_passport']):'';
$tel =(isset($_POST['tel']))? trim ($_POST['tel']):'';
$work_contact =(isset($_POST['work_contact']))? trim ($_POST['work_contact']):'';
$address =(isset($_POST['address']))? trim ($_POST['address']):'';
$work_address =(isset($_POST['work_address']))? trim ($_POST['work_address']):'';
$comments =(isset($_POST['comments']))? trim ($_POST['comments']):'';
$cell =(isset($_POST['cell']))? trim ($_POST['cell']):'';
//cell convert to 27 


if(isset($_POST['submit'])){
foreach($_POST['name'] as $name)
foreach ($_POST['surname'] as $surname)
foreach($_POST['parent_type'] as $parent_type )


$count = count($_POST['name']);

if(is_array($name) && is_array($surname) && is_array($parent_type)) {

    for($i = 0; $i < $count; $i++) {


//$name =  $_POST['name'][$i], $_POST['surname'][$i] , $_POST['parent_type'][$i];
//echo $name; 
///
#########################################################
$query ='INSERT INTO dependants(
name, surname, othername, gender, id_passport, 
cell, tel, work_contact, address, work_address, comments, 
memberid,companyid, joined_date, usercode, recieved_by , deadalive
)
VALUES
(
:name, :surname, :othername, :gender, :id_passport, 
:cell, :tel, :work_contact, :address, :work_address, :comments, 
:memberid,:companyid, :joined_date, :usercode, :recieved_by ,:deadalive
)';
$insert=$con->prepare($query);
$insert->execute(array(
':name'=>$name,
':surname'=>$surname,
':othername'=>$othername,
':gender'=>$gender,
':id_passport'=>$id_passport,
':cell'=>$realcell,
':tel'=>$tel,
':work_contact'=>$work_contact,
':address'=>$address,
':work_address'=>$work_address,
':comments'=>$comments,
':memberid'=>$memberid,
':usercode'=>$usercode,
':joined_date'=>$joined_date,
':companyid'=>$companyid,
':recieved_by'=>$recieved_by,
':deadalive'=>$deadalive
));
}}}

if($query){
echo '<h1 style="color:green">Dependant Created</h1>';
}

2 Answers 2

1

I think there are a few problems, the first is most of this code is redundant...

foreach($_POST['name'] as $name)
foreach ($_POST['surname'] as $surname)
foreach($_POST['parent_type'] as $parent_type )


$count = count($_POST['name']);

As the foreach() has no {} all the loops will eventually just execute the last line, which will always be the same. So remove the 3 foreach() loops and just have

$count = count($_POST['name']);

Then to set the values for each loop, you almost had it (I think) and need...

$name =  $_POST['name'][$i];
$surname = $_POST['surname'][$i];

Finally your test to see if it has worked

if($query){

is incorrect, you should check that all of the

$insert->execute(array(...)); 

statements return true and this should show that they have all been created.

Sign up to request clarification or add additional context in comments.

7 Comments

Can you please compile the entire code as a single unit instead of part part .I appreciate your effort sir thanks a lot
There isn't much to it, I would have thought being able to make the few changes was much easier than huge chunks of code.
i made changes and it inserted array text and it gave me this error : Array to string conversion in . I think that is now my problem since you managed to drive me to the solution .
One thing is if you can identify the line with the error, it's easier to find out whats causing it - if you need error reporting - stackoverflow.com/questions/1053424/…
I think we have now to convert the array in real names not to post the words Array in the bd
|
0

I finaly got the answer by the help of @Nigel Ren , all is fine am sorted here is the answer .

if(isset($_POST['submit'])){
$surname = $_POST['surname'];
$name = $_POST['name'] ;
$parent_type = $_POST['parent_type'];
$count = count($_POST['name']);

if(is_array($name) && is_array($surname) && is_array($parent_type)) {
    for($i = 0; $i < $count; $i++) {
     var_dump($name[$i]);

$surname1 = $surname[$i];
$name1 =$name[$i];
$parent_type1 = $parent_type[$i];

///
#########################################################
$query ='INSERT INTO dependants(
name, surname, othername, gender, id_passport, 
cell, tel, work_contact, address, work_address, comments, 
memberid,companyid, joined_date, usercode, recieved_by , deadalive,parent_type
)
VALUES
(
:name, :surname, :othername, :gender, :id_passport, 
:cell, :tel, :work_contact, :address, :work_address, :comments, 
:memberid,:companyid, :joined_date, :usercode, :recieved_by ,:deadalive,:parent_type
)';
$insert=$con->prepare($query);
$insert->execute(array(
':name'=>$name1,
':surname'=>$surname1,
':othername'=>$othername,
':gender'=>$gender,
':id_passport'=>$id_passport,
':cell'=>$realcell,
':tel'=>$tel,
':parent_type'=>$parent_type1,
':work_contact'=>$work_contact,
':address'=>$address,
':work_address'=>$work_address,
':comments'=>$comments,
':memberid'=>$memberid,
':usercode'=>$usercode,
':joined_date'=>$joined_date,
':companyid'=>$companyid,
':recieved_by'=>$recieved_by,
':deadalive'=>$deadalive
));
}}}// all end 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.