Skip to content
Advertisement

Taking Array Post Data and INSERTING to sql

I am trying to get an Array of $_POST data and inserting it into mysql with 3 insert statements .. to make 3 new rows in a table.

Form: The Variable in the name field is the Workshop ID number.

        <div class="form-group row">
        <label for="make" class="col-sm-2 col-form-label">Appropriate</label>
        <div class="col-sm-4"><input type="text" name="appropriate[<?php echo $theassign?>]"  class="form-control" >
        </div>
    </div>
    <div class="form-group row">
        <label for="model" class="col-sm-2 col-form-label">Useful</label>
        <div class="col-sm-4"><input type="text" name="useful[<?php echo $theassign?>]"  class="form-control" >
        </div>
    </div>
    <div class="form-group row">
        <label for="serial" class="col-sm-2 col-form-label">understandable</label>
        <div class="col-sm-4"><input type="text" name="understandable[<?php echo $theassign?>]"  class="form-control" >
        </div>
    </div> 

The final mysql statement will be something like the following where $workshopid would come from the numbers inside the brackets in the array

INSERT INTO evaluation (workshopid, appropriate, useful, understandable) VALUES ($workshopid, $appropriate, $useful, $understandable)

INSERT INTO evaluation (workshopid, appropriate, useful, understandable) VALUES ($workshopid, $appropriate, $useful, $understandable)

INSERT INTO evaluation (workshopid, appropriate, useful, understandable) VALUES ($workshopid, $appropriate, $useful, $understandable)

Currently, print_r($_POST); yeilds:

Array ( [appropriate] => Array ( [12] => 1 [4] => 2 [11] => 3 ) [useful] => Array ( [12] => 1 [4] => 2 [11] => 3 ) [understandable] => Array ( [12] => 1 [4] => 2 [11] => 3 ) [helpful] => Array ( [12] => 1 [4] => 2 [11] => 3 ) )

Can somebody help with the foreach loop?

THIS gets me close

$keys = array_keys($_POST); 
for($i = 0; $i < count($_POST); $i++) { 
    echo $keys[$i] . "<br>"; 
    foreach($_POST[$keys[$i]] as $key => $value) { 
        echo $key . ":" . $value . "<br>"; 
    } 
    echo "<br>"; 
}

but I don’t want to group the ‘appropriate’ .. together .. I want to group the 12s together, as THAT is the $workshopid.

appropriate
12:1
4:2
11:3

useful
12:1
4:2
11:3

understandable
12:1
4:2
11:3

helpful
12:1
4:2
11:3

Advertisement

Answer

Considering the values to always be a number, I’d do something like this:

$inserts = array();
foreach($_POST['appropriate'] as $workshipid => $appropriate){
    $appropriate = (int) $appropriate;
    $useful = (int) $_POST['useful'][$workshipid];
    $understandable = (int) $_POST['understandable'][$workshipid];
    $inserts[] = "($workshopid, $appropriate, $useful, $understandable)";
}

//Query:
if($inserts){
    $query = "INSERT INTO evaluation (workshopid, appropriate, useful, understandable) VALUES " . implode(',', $inserts);
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement