如何使用 AngularJs $stateChangeSuccess 将数据填充到 MySQL 使用 PHP?

How can use AngularJs $stateChangeSuccess to populate data to MySQL using PHP?

我有一个用数组列出对象的函数:

//onload event-- to set the values
$scope.$on('$stateChangeSuccess', function () {

    $scope.cart=sharedCartService.cart;
    $scope.total_qty=sharedCartService.total_qty;
    $scope.total_amount=sharedCartService.total_amount;     
});    

我需要获取所有数据并将所有数据插入(填充)到数据库中。我使用 MySQL 和 PHP。

谢谢。

您创建了一个文件,假设其名称为 save.php

在该文件中,您将拥有类似

的内容
header("Access-Control-Allow-Origin: *");
Global $db;
$db = new PDO('mysql:dbname=databasename;host=localhost', 'dbuser', 'dbpassword');
if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400');    // cache for 1 day
}

// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
        header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

    exit(0);
}
$postdata = file_get_contents("php://input");
if (isset($postdata)) {
    $request = json_decode($postdata);
    $cart = $request->cart;
    $total_qty = $request->total_qty;
    $total_amount = $request->total_amount;

}
else {
    echo "Not called properly!";
}
$query = $db->prepare("
                INSERT INTO yourtable
                    (cart, total_qty, total_amount)
                VALUES
                    (:cart, :total_qty, :total_amount)");

$query->execute(array(
                ':cart' => $cart,
                ':total_qty' => $total_qty,
                ':total_amount' => $total_amount));

并且在 Angular (stateChangeSuccess) 中的函数中,您发出 post 请求,例如 http://localhost:8080/save.php

$http.post(url, data, config)
.then(
   function(response){
     // success callback
   }, 
   function(response){
     // failure callback
   }
 );