micky
micky

Reputation: 317

Storing in same id of database

I have this form to allow users to insert multiple phone types and multiple emails:

<div id="dynamicInput">
          <input type="text" name="mytype[]" placeholder="Phone Type"> <input type="text" name="myinputs[]" id="phone" placeholder="Phone Number">
      <span class="glyphicon glyphicon-plus" onClick="addInput('dynamicInput');"></span>
<br>  </div><br>

And in the controller i have this piece of code:

    foreach($myinputs as $myinput)
    {
        $phone=new phone();
        $phone->secondary_phones=$myinput;
        $phone->id=$getlast;
        $phone->save();

    }

   foreach($mytype as $mytypes)
        {
                $phone=new phone();
                $phone->id=$getlast;
                $phone->secondary_phones_type=$mytypes;
                $phone->save();
        }

$getlast is the id from another table.

the problem here is phone types and phone numbers are added in the separate phone_id. what I want is respective phone type and phone number to be inserted in same phone_id. phone_id is auto increment primary key. I have secondary_phones and secondary_phones_type as the database columns, I guess the problem is with saving in database differently for phone number and phone type.

Upvotes: 0

Views: 89

Answers (1)

Yasin Patel
Yasin Patel

Reputation: 5731

If you have same number of inputs for phone types and phone numbers than you can try it.

for($i=0;$i<$myinputs.length;$i++)
{
  $phone=new phone();
  $phone->secondary_phones=$myinputs[$i];
  $phone->secondary_phones_type=$mytype[$i];
  $phone->id=$getlast;
  $phone->save();
}

Upvotes: 1

Related Questions