Ad

Laravel 5: Insert (nested) Array Data From Form In Mysql Db (convert Syntax From Mysqli)

- 1 answer

I am having problems converting existing mysqli syntax to Laravel suitable format. I am happy to make use of raw queries as long as it works.

I have this mysqli syntax (which works - but not in Laravel obviously):

$outID = array_map($mysqli_escape, $_POST['outcomeid']);
$chID = array_map($mysqli_escape, $_POST['ChallengeRandom']);
$pts = array_map($mysqli_escape, $_POST['PropOutcomePts']);
$userID = array_map($mysqli_escape, $_POST['username']);
$timestamp = array_map($mysqli_escape, $_POST['ctime']);
$prID = array_map($mysqli_escape, $_POST['PropId']);
$crID = array_map($mysqli_escape, $_POST['CrushId']);

for($i=0;$i<count($outID);$i++) { 
$ins1 = 'INSERT INTO challenge_input (PropOutcomeId, ChallengeId, ChallengeUserId, InputTime, PropOutcomePts, PropId, CrushId) VALUES (\''.$outID[$i].'\', \''.$chID[$i].'\', \''.$userID[$i].'\', \''.$timestamp[$i].'\', \''.$pts[$i].'\', \''.$prID[$i].'\', \''.$crID[$i].'\')';
$query = mysqli_query($con,$ins1) or die(mysqli_error($con));
} 

The form output from the previous page looks like this (context: the form contains five questions, each with four answer options (which have outcomeid and outcomepts). Only if an outcomeid is checked - i.e., the answer that is chosen by the user - is an outcomeid passed in the form.):

Crushtime[]:2016-01-28 16:00:00
PropId[]:35
CrushId[]:13
username[]:16
ctime[]:2016-01-08 15:55:28
ChallengeRandom[]:db167049-b617-11e5-a
outcomeid[]:45
PropOutcomePts[]:200
PropOutcomePts[]:100
PropOutcomePts[]:100
PropOutcomePts[]:200
PropId[]:36
CrushId[]:13
username[]:16
ctime[]:2016-01-08 15:55:28
ChallengeRandom[]:db167049-b617-11e5-a
PropOutcomePts[]:100
PropOutcomePts[]:100
PropOutcomePts[]:50
outcomeid[]:52
PropOutcomePts[]:200
PropId[]:38
CrushId[]:13
username[]:16
ctime[]:2016-01-08 15:55:28
ChallengeRandom[]:db167049-b617-11e5-a
PropOutcomePts[]:100
outcomeid[]:54
PropOutcomePts[]:50
PropOutcomePts[]:100
PropOutcomePts[]:150
PropId[]:39
CrushId[]:13
username[]:16
ctime[]:2016-01-08 15:55:28
ChallengeRandom[]:db167049-b617-11e5-a
PropOutcomePts[]:100
PropOutcomePts[]:50
outcomeid[]:59
PropOutcomePts[]:100
PropOutcomePts[]:150
PropId[]:40
CrushId[]:13
username[]:16
ctime[]:2016-01-08 15:55:28
ChallengeRandom[]:db167049-b617-11e5-a
PropOutcomePts[]:200
PropOutcomePts[]:100
PropOutcomePts[]:50
outcomeid[]:64
PropOutcomePts[]:100

Code from comment

foreach ($outID as $key => $n) {
    DB::insert('INSERT INTO challenge_input (PropOutcomeId, ChallengeId, ChallengeUserId, InputTime, PropOutcomePts, PropId, CrushId) VALUES (?,?,?,?,?,?,?)',
        array('outID' => $outID[$key], 'chID' => $chID[$key], 'userID' => $userID[$key], 'timestamp' => $timestamp[$key], 'pts' => $pts[$key], 'prID' => $prID[$key], 'crID' => $crID[$key]));
}
Ad

Answer

The keys for the array must match the column names:

foreach ($outID as $key => $n) {
    DB::table('challenge_input')->insert(array('PropOutcomeId' => $outID[$key], 'ChallengeId' => $chID[$key], 'ChallengeUserId' => $userID[$key], 'InputTime' => $timestamp[$key], 'PropOutcomePts' => $pts[$key], 'PropId' => $prID[$key], 'CrushId' => $crID[$key]));
}
Ad
source: stackoverflow.com
Ad