Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
Do you have the inner part of the code shown inside the form element multiple times – or do you have multiple form elements? –  CBroe Apr 29 at 22:26
    
@CBroe inside the form multiple times. –  Noobtastic Apr 29 at 22:28

3 Answers 3

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
    )); 
share|improve this answer

It looks like you're not properly transferring the POST data into the variable. By my understanding, you are trying to receive an array of data from POST, but are only getting one value assigned to your PHP variable (the last value of the array). This is due to the way you handle the data in the foreach statement. Instead of assigning each instance of the foreach statement, you're simply running the foreach and grabbing the last value remaining from the loop. Instead, you can simply assign the values directly without a foreach, as in:

$phonenos = $_POST['phonenos'];
$phonetype = $_POST['phonetypes'];

This should result in $phonenos being an array, assuming the POST data was also an array.

However, you may have to redesign your insert query to handle an array of data, unless you plan to enter all elements in the array into a single column (which could be done using serialization, as has been mentioned). If you plan to insert multiple rows, then you will have to handle in a loop.

Edit:

If the keys are related, you can use a foreach loop to cycle through the execute statement with the appropriate values. I'm not terribly familiar with PDO syntax (mysqli user, myself), but based on your structure you could run this loop after preparing the statement.

foreach($phonenos as $key => $value)
{
    $query->execute( array(
        ':p_id'=>$lastid,
        ':phoneno'=>$value,
        ':phonetype'=>$phonetype[$key]
    )); 
}
share|improve this answer
    
so would my query insert statement be :phonenos with binding of $phonenos? –  Noobtastic Apr 29 at 22:35
    
This depends on how you want the data stored. Do you want all of the data stored in a single row or in multiple rows? –  Mitch Goshorn Apr 29 at 22:36
    
I would like it in multiple rows. –  Noobtastic Apr 29 at 22:37
    
And based on your design the keys to the $phoneno and $phonetype will be correlated? (meaning $phoneno[2] will have a type equal to $phonetype[2]) –  Mitch Goshorn Apr 29 at 22:39
    
Yes, that is correlated. @Mitch Goshorn –  Noobtastic Apr 29 at 22:41
up vote 0 down vote accepted

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
    ));
}    
share|improve this answer
    
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 5 at 9:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.