0
Array
(
[pid] => Array
    (
        [0] => 2
        [1] => 3
    )

[price] => Array
    (
        [0] => 20
        [1] => 20
    )

[qty] => Array
    (
        [0] => 2
        [1] => 1
    )

)

i have an outcome of the above array from some processing. with this i need to update to database like below table

pid price qty
2   20    2
3   20    1 
2
  • 3
    so what is your problem? Commented Mar 27, 2013 at 11:58
  • So Update it and let us know if you run into an issue ... Commented Mar 27, 2013 at 12:01

4 Answers 4

1
$i = 0;
while( $i < count( $YourArray['pid']) ) {
    $query = "INSERT INTO `tableName`(`pid`, `price`, `qty`) VALUES( ?, ?, ? )";
    $stmt = $con->prepare( $query );
    $stmt->execute(
        array(
            $YourArray['pid'][$i],
            $YourArray['price'][$i],
            $YourArray['qty'][$i]
        )
    );
    $i++;
}

Where, I used the method of insertion.

Sign up to request clarification or add additional context in comments.

Comments

0
for(i=0;i<amount;i++){
    echo $array['pid'][i];
    echo $array['price'][i];
    echo $array['qty'][i]; 
}

Where amount must be a count of the amount of rows you have

Comments

0

Try this :

$array = array("pid" => array(2,3),"price" => array(20,20),"qty" => array(2,1));
array_unshift($array, null);
$res   = call_user_func_array('array_map', $array);

echo "<pre>";
print_r($res);

Output :

Array
(
    [0] => Array
        (
            [0] => 2
            [1] => 20
            [2] => 2
        )

    [1] => Array
        (
            [0] => 3
            [1] => 20
            [2] => 1
        )

)

loop this array and add to DB - So that you can add two entries in DB

2 Comments

How does this update a database? He obviosly already has the output in an array.
I just processed the array so that using a loop he can update to DB
0

this is a wrong way of doing it, i would use an indexed array, and then build a foreach loop that will handle each 1 separately, something like:

$values = array();
$values[] = array(
                  'pid' => 2,
                  'price' => 20,
                  'qty' => 2
                 );

$values[] = array(
                  'pid' => 3,
                  'price' => 20,
                  'qty' => 1
                  );

and from this then build a foreach loop and run each query there

foreach ($values as $value) {
   $query = "insert into blah 
              set pid = " . $value['pid'] . ",
              price = " . $value['price'] . ",
              qty = " . $value['qty'] . ";";

   mysql_query($query);
}

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.