0

I have a form like bellow with more then 1 Input in my form with 1 name like bellow:

<form>
<input type="text" name="family">
<input type="text" name="inputCurrName[]">
<input type="text" name="inputCurrName[]">
<input type="text" name="inputCurrName[]">
<input type="submit" name="submit" value="submit">
</form>

and i have a table with 2 column with column name Family and CurrName

I want to save multiple names into one Row of CurrName Column in form submission , for example this is my data when submitted:

Family Input: ali inputCurrName[] Inputs : array(jack,peter,lara)

I want to save only 1 record in database with that data like:

familyColumn Value: ali | CurrNameColumn Value: {jack,peter,lara}

I have used code below but it save 3 record in database!

$input = $request->all();
for ($i = 0; $i <= count($input['inputCurrName']); $i++) {
  $user_data = [
    'family' => $input['family'],
    'curr_name' => $input['inputCurrName'][$i],
  ];

  User::Creat($user_data);
}

What should i DO?

2 Answers 2

3

You are doing a for loop checking the length of inputCurrName, so it loops through each inputCurrName, which is why you are getting 3 records per FamilyName. In this case, you would only care about 1 record per FamilyName.

There are a few ways you can insert the array of inputCurrName values into 1 record with FamilyName.

You can use serialize() to create a storable representation of a value and unserialize() to decode it when retrieving from the database.

-OR-

You can use implode() to create a comma delimited list when inserting the record. And upon retrieval from the database, use explode() to iterate through each of the comma delimited values.

-OR-

If you prefer json you can do the same with json_encode() and json_decode() the same way as serialize() and unserialize()

+------------+---------------------------------------------------------+
| FamilyName |                        CurrName                         |
+------------+---------------------------------------------------------+
| Ali        |  a:3:{i:0;s:5:"Laura";i:1;s:6:"Peter ";i:2;s:4:"Jack";} |
| Ali        |  Laura,Peter,Jack                                       |
+------------+---------------------------------------------------------+

Using the following code will give you an example for the table above. curr_name_optional is part of the $user_data array to showcase implode().

<?php
if(isset($_POST) && count($_POST) > 0) {

    $inputFamilyName  = $_POST['family'];
    $inputCurrentName = $_POST['inputCurrName'];

    // Your laravel code here..
    $user_data = [
      'family' => $inputFamilyName,
      'curr_name' => serialize($inputCurrentName),
      'curr_name_optional' => implode(',', $inputCurrentName),
      'curr_name_optional_1' => json_encode($inputCurrentName)
    ];

    echo "<pre>";
    print_r($user_data);
    echo "</pre>";
}
?>

<form method="post">
    Family Name: <input type="text" name="family"/> <br/>
    Curr Name: <input type="text" name="inputCurrName[]"/><br/>
    Curr Name: <input type="text" name="inputCurrName[]"/><br/>
    Curr Name: <input type="text" name="inputCurrName[]"/><br/>
    <input type="submit" name="submit" value="submit"/>
</form>

Also, I would make sure to trim and sanitize your form input data before inserting into the database.

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

3 Comments

Thank You Very Much @Woodrow , all of your 3 ways worked! . which way is better and standard ?
Can You Tell Me How Can I Show serialize,implode,json_encode Data In Laravel Blade In Pretty View Like This?>> FamilyName:: Ali , CurrName:: Laura,Peter,Jack
I want to show in laravel blade like this : link
1

You can simply use a foreach and then convert the data into json format then insert into a single row

$arr = [];
foreach($request->get('inputCurrName') as $key => $value){
   $arr[$key] => $value;
}
$user_data = [
      'family' => $request->get('family_name'),
      'curr_name' => json_encode($arr),
     ];

User::Create($user_data);

Not sure how its working but, you seem to forgot passing csrf-token in your form. Add @csrf just below the <form> tag.

1 Comment

Thank you , @Woodrow ways worked , and i have tested your solution but it has syntax error on $arr[$key] => $arr[$value];

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.