简体   繁体   中英

How to insert multidimensional array in php?

Just a follow-up question from my previous question . By getting the values from the array, my code is this:

       for ($i=0; $i<count($entries);$i++)
            {
                $pcode = substr($entries[$i][0],0,-2);
                $sku = substr($entries[$i][0],2);
                $aprice = $entries[$i][1];
                $beginv = $entries[$i][2];
                $delivery = $entries[$i][3];
                $endinv = $entries[$i][4];
                $offtake = $entries[$i][5];
                $bo = $entries[$i][6];
                $shelf = $entries[$i][7];
                $gondola = $entries[$i][8];
                $chillers = $entries[$i][9];
                $mass = $entries[$i][10];
                $other = $entries[$i][11];
                $total = $entries[$i][12];

                $statement = "INSERT INTO `report_details` VALUES ('$id', '$pcode', '$sku', '$aprice', '$beginv', '$delivery', '$endinv', '0', '0', '$offtake, '0', '0', '0', '$bo', '$shelf', '$gondola', '$chillers', '$mass', '$other', '$total')";
                $result = mysqlparseradd($statement,$db);

                if($result)
                {echo "YES";}
                else
                {echo "NO";}    
            }

And my array is like this:

Array
 (
    [0] => Array
        (
            [0] => 0111
            [1] => 260.00
            [2] => 23
            [3] => 34
            [4] => 3
            [5] => 54
            [6] => 1
            [7] => 2
            [8] => 4
            [9] => 5
            [10] => 12
            [11] => 23
            [12] => 46
        )

    [1] => Array
        (
            [0] => 0214
            [1] => 22.00
            [2] => 32
            [3] => 4
            [4] => 11
            [5] => 25
            [6] => 4
            [7] => 12
            [8] => 23
            [9] => 5
            [10] => 2
            [11] => 2
            [12] => 44
        )

    [2] => Array
        (
            [0] => 0313
            [1] => 25.00
            [2] => 5
            [3] => 52
            [4] => 12
            [5] => 45
            [6] => 12
            [7] => 5
            [8] => 6
            [9] => 7
            [10] => 12
            [11] => 3
            [12] => 33
        )

)

The thing with this, I always get the value of NO which means that it doesn't get added to the database. My question is, what is wrong with the code that I have that it doesn't get inserted into the database? I did fine when I inserted only one row of data but not having multiple rows of data.

EDIT

When I print my $statement the result is this:

INSERT INTO `report_details` VALUES ('JJ1234567890_140822_140824_1020001', '01', '11', '260.00', '23', '34', '3', '0', '0', '54, '0', '0', '0', '1', '2', '4', '5', '12', '23', '46')

Only one value of array is being added. My mysqlparseradd is a function that acts like the mysqli_query . It executes the query.

You have a syntax error.

Use this.

$statement = "INSERT INTO `report_details` VALUES ('$id', '$pcode', '$sku', '$aprice', '$beginv', '$delivery', '$endinv', '0', '0', '$offtake', '0', '0', '0', '$bo', '$shelf', '$gondola', '$chillers', '$mass', '$other', '$total')";

An alternative way to your current solution, using array map and a function to build up an array of values clauses and inserting them once.

<?php

$statement = "INSERT INTO `report_details` VALUES ".implode(',', array_map(function($entry) use ($id) {return(format_row($id, $entry));}, $entries));
$result = mysqlparseradd($statement,$db);

if($result)
{
    echo "YES";
}
else
{
    echo "NO";
}   

function format_row($id, $entry)
{
    $pcode = substr($entry[0],0,-2);
    $sku = substr($entry[0],2);
    $aprice = $entry[1];
    $beginv = $entry[2];
    $delivery = $entry[3];
    $endinv = $entry[4];
    $offtake = $entry[5];
    $bo = $entry[6];
    $shelf = $entry[7];
    $gondola = $entry[8];
    $chillers = $entry[9];
    $mass = $entry[10];
    $other = $entry[11];
    $total = $entry[12];
    return "('$id', '$pcode', '$sku', '$aprice', '$beginv', '$delivery', '$endinv', '0', '0', '$offtake', '0', '0', '0', '$bo', '$shelf', '$gondola', '$chillers', '$mass', '$other', '$total')";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM