0

I'm having trouble extracting arrays to insert into the database. My form accepts multiple and dynamic number of inputs so I have the data in an array with inputs phonenos[] and phonetypes[]:

<form name="add" action="" method="POST">
<input name="name" type="text" placeholder="Name"></input><br />
    <input name="qty" type="text" placeholder="Qty"></input><br /> -->
    <input class="form-control required" name="phonenos[]" maxlength="14" type="text" placeholder="number..."><br>
    <select class="form-control" name="phonetypes[]">
        <option value="0">Choose a phone type</option>
        <option value="Main">Main</option>
        <option value="Fax">Fax</option>
        <option value="Mobile/Direct">Mobile/Direct</option>
    </select>
    <div id="addmore">
        <input type="button" value="Add More" onClick="addRow(this.form)"></input>
    </div>
    <input type="submit" value="submit" name="action"></input>
</form>

In my PDO query:

..... first query insertion...

$phonenos = $_POST['phonenos'];

foreach($_POST['phonenos'] as $phoneno) {
    $phoneno;
}

$phonetypes = $_POST['phonetypes'];

foreach($_POST['phonetypes'] as $phonetype) {
    $phonetype;
}

$sql = 'INSERT INTO phone (p_id, phoneno, phonetype) values (:p_id, :phoneno, :phonetype)';
$query = $conn->prepare($sql);
$query->execute( array(
        ':p_id'=>$lastid,
        ':phoneno'=>$phoneno,
        ':phonetype'=>$phonetype
    )); 

So I did a var_dump on variables $phoneno and $phonetype after a submission of multiple phone numbers and it only printed out the last number and type whereas I wanted the entire list that was submitted. How do I get all the data so I can insert it into the database?

Noobtastic
  • 187
  • 1
  • 3
  • 18

2 Answers2

0

If you want all the numbers inserted in the same column, then the best solution I can think is to serialize them before you insert in db.

$phonenos = serialize($_POST['phonenos']);
$phonetypes = serialize($_POST['phonetypes']);

$sql = 'INSERT INTO phone (p_id, phoneno, phonetype) values (:p_id, :phoneno, :phonetype)';
$query = $conn->prepare($sql);
$query->execute( array(
        ':p_id'=>$lastid,
        ':phoneno'=>$phonenos,
        ':phonetype'=>$phonetypes
    )); 
Ibrahim
  • 2,034
  • 15
  • 22
0

I figured out the solution through a for loop.

$phones = $_POST['phones'];
$phonetypes = $_POST['phonetypes'];

for($i = 0, $j = 0; $i <= $phones, $j <= $phonetypes; $i++, $j++) {
    $phone = $phones[$i];
    $phonetype = $phonetypes[$j];

    $sql = "INSERT INTO phone (p_id, phone, phonetype) values (:p_id, :phone, :phonetype)";
    $query = $conn->prepare($sql);
    $query->execute( array(
        ':p_id'=>$lastid,
        ':phone'=>$phone,
        ':phonetype'=>$phonetype
    ));
}    
Gwenc37
  • 2,064
  • 7
  • 18
  • 22
Noobtastic
  • 187
  • 1
  • 3
  • 18
  • No need to prepare the same query over and over again. You can move the `$sql = ...; $query = $conn->prepare($sql);` lines out of the loop, they only need to be run once. – Gerald Schneider Aug 05 '14 at 09:11