0

I have a form that someone can add multiple phone numbers and extension, and I am trying to save them in the database, whenever I save the data it shows the word "Array" in both filed.

Here is my code so far:

if (!empty($_POST['Phone']) && isset($_POST['Extension'])) {
foreach ($_POST['Phone'] as $key => $value) {
    foreach ($_POST['Extension'] as $key => $value2) {
        $para = array("UserID" => \PDO::PARAM_INT, "Phone" => \PDO::PARAM_STR, "Extension" => \PDO::PARAM_STR);
        $val = array($_SESSION['UserID'], $_POST['Phone'], $_POST['Extension']);
        $r = DB::Call("[spPhoneInsert]", $para, $val);           
    }
}

if (count($r) > 0 && $r[0]['Result'] == 'Ok') {
    header("location:home.php?added_phone=1");
} else {
    header("location:home.php?error_phone=1");
}
exit;
}
4
  • What problem you are facing in it ? Commented Mar 7, 2017 at 6:30
  • whenever, I post the data from the table the result show as Array not the real values I posted Commented Mar 7, 2017 at 6:43
  • why are you looping extension inside phone's loop, do you have multiple extensions for each phone or something? Commented Mar 7, 2017 at 6:50
  • each phone have one extension Commented Mar 7, 2017 at 6:58

1 Answer 1

1

whenever you post a form with multiple same name elements like

<input type="text" name="phone[]">
<input type="text" name="phone[]">

data is posted as array to the action page. You have to loop through this array to save them in data base

you can loop like this and create whatever format you want Following code will save your number and extension comma seprated

    foreach ($_POST['phone'] as $number){
    $allNumbers .= $number.',';
    }
foreach ($_POST['extension'] as $ext){
    $allExt .= $ext.',';
    }
  $para = array("UserID" => \PDO::PARAM_INT, "Phone" => \PDO::PARAM_STR, "Extension" => \PDO::PARAM_STR);
        $val = array($_SESSION['UserID'], trim($allNumbers,','), trim($allExt,','));
        $r = DB::Call("[spPhoneInsert]", $para, $val); 
Sign up to request clarification or add additional context in comments.

5 Comments

I already have the my input like this <input type="text" name="Phone[]">
thats right but on action page you your $_POST['phone'] is available as array thats why it is insert as array in your column
The result of inserting data in the form shows the word "Array" and I want to view the data inserted. how can I change it to insert the data i submited in the form, and not show me the word array
when I echo it, it will show the inserted data, but its not going to the database
I tried to add two arrays by using the code. the first one was added but the value was empty in the database, and the second one wasn't added

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.