user1697533 user1697533 - 26 days ago 13
Javascript Question

How to Enter angularJs Array to MySQL Database?

I have this function with array object that has all the Cart Orders

createMyOrder: function (myOrder, callback, err) {
var data = myOrder;

console.log(JSON.stringify(data));

return $http({
url: jsonurl + 'cRtOrder',
method: "POST",
data: data
}).success(callback).error(err);
},


This is what
console.log(JSON.stringify(data));
shows.

[{"cart_item_id":"94","cart_item_image":"super-foods-400x400.jpg","cart_item_name":"Chicken Comma","cart_item_price":"77","cart_item_qty":1},{"cart_item_id":"89","cart_item_image":"nachos10.jpg","cart_item_name":"Rice with fish","cart_item_price":"9.87","cart_item_qty":1}]


Now how can i send this array to the database using MySQL. using

//Repetitive Insert command on each row

or

//Single Insert command by concatenating all array values into array

I don't how to. Please if anyone would help I will be so grateful.Thanks

Answer

You need to convert json string to php array in your php file:

 $json = file_get_contents('php://input');
 $obj = json_decode($json,true);

After that you could use a foreach to iterate your array:

foreach($obj as $item){
  echo $item['cart_item_id'];
}

Where i wrote the echo you must insert in the database with the cells you want

Comments