0

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.

13
  • Use $mysqli->error to see whats the problem is. I dont know whats your mysqlparseradd do and so i cant help you. Commented Sep 10, 2014 at 9:21
  • 1
    And please print the $statement. Commented Sep 10, 2014 at 9:26
  • @PatrickB please see my question again Commented Sep 10, 2014 at 9:54
  • Please use var_dump($db->error); after mysqlparseradd and post it :) Commented Sep 10, 2014 at 9:58
  • 1
    BTW, it's much more efficient to build the values in the loop, and then submit one big, multi-row insert at the end, rather than all these one-row inserts. Commented Sep 10, 2014 at 10:00

2 Answers 2

2

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')";
Sign up to request clarification or add additional context in comments.

2 Comments

Oh! I see! Thank you! That was such a simple '. My mistake.
:) Mark please if finished :D
1

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')";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.