如何在Angular.js中将数据发送到SQL数据库?

时间:2018-03-11 05:49:32

标签: javascript php angularjs

当我在localhost服务器上运行我的代码时,它会向我发送一个空数组,我正在尝试使用isset但是它也没有工作。

var myApp = angular.module('myApp', ['ngRoute']);
myApp.controller("FormController", function($scope, $http) {

    $scope.submission = {};
    $scope.insertData = function() {
        $http({
            method: "POST",
            url: "index.php",
            data: $scope.submission,
        }).success(function(data) {

            if (data.error) {
                $scope.errororder = data.error.order;
                $scope.errorordertotal = data.error.ordertotal;
                $scope.errorCardtype = data.error.Cardtype;
                $scope.errorCreditCard = data.error.CreditCard;
                $scope.errorNameonCard = data.error.NameonCard;
                $scope.errorCVD = data.error.CVD;
                $scope.errorExpiry = data.error.Expiry;
            } else {
                $scope.insert = null;
                Sscope.errororder = null;
                Sscope.errorordertotal = null;
                Sscope.errorCardtype = null;
                Sscope.errorCreditCard = null;
                Sscope.errorNameonCard = null;
                Sscope.errorCVD = null;
                Sscope.errorExpiry = null;


            }
        });
    }
});
* {
    box-sizing: border-box;
}

.payment {
    display: block;
    background: #f4f4f4;
    max-width: 850px;
    margin-top: 20px;
    margin-left: auto;
    margin-right: auto;
    padding: 20px 40px;
    text-align: center;
    position: relative;
    font-family: sans-serif;
    font-size: 15px;
}

.form1 {
    text-align: left;
    color: blue;
    font-size: x-large;
    font-family: 'Times New Roman', Times, serif;
    width: auto;
}

.textintro h1 {
    font-size: 40px;
    font-weight: 200;
    line-height: 100%;
    margin-bottom: 20px;
    padding-top: 10px;
    font-weight: 700;
    font-family: sans-serif;
    color: slateblue;
    margin-top: 20px;
}

.textintro p {
    font-style: normal;
}

input {
    width: 100%;
    display: inline-block;
    font-weight: 100;
    padding: 15px;
    margin: 5px 0;
    font-size: 1rem;
    color: darkgray;
}

.submission {
    display: inline-block;
    padding: 12px 24px;
    margin-bottom: 0;
    font-size: 1.4rem;
    text-align: center;
    vertical-align: middle;
    cursor: pointer;
    color: gray;
    min-width: 26px;
    min-height: 26px;
    background-color: firebrick;
}

.error {
    margin-top: 0px;
    color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!Doctype html>
<html lang="en" ng-app="myApp">

<head>
    <meta charset="UTF-8">
    <title>Sample Payment Processing with Moneris Integration </title>
    <link rel="stylesheet" href="app.css">
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.0-beta.3/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular-route.min.js"></script>
    <script type="text/javascript" src="app.js"></script>
    <h1 align="center">Sample Payment Processing with Moneris Integration</h1>

</head>


<body class="body1">
    <div class="payment" ng-app="myApp" ng-controller="FormController">
        <div class="textintro">
            <p ng-show="msg">{{msg}}</p>
        </div>
        <form name="form1" class="form1" ng-submit="insertData()">

            <label>Order:</label><input ng-model="submission.Order" type="number" name="order" ng-required="true"><br>
            <p class="error" ng-show="errororder">{{errororder}}</p>
            <br><label>Order Total:</label> <input ng-model="submission.Ordertotal" type="number" name="Ordertotal" ng-required="true"><br>
            <p class="error" ng-show="errorOrdertotal">{{errorOrdertotal}}</p>
            <br><label>Card type:</label> <input ng-model="submission.Cardtype" type="text" name="visa" ng-required="true"><br>
            <p class="error" ng-show="errorCardtype">{{errorCardtype}}</p>
            <br> <label>Credit Card #:</label><input ng-model="submission.CreditCard" type="number" name="importantnumber" ng-required="true"><br>
            <p class="error" ng-show="errorCreditCard">{{errorCreditCard}}</p>
            <br><label>Name on card:</label><input ng-model="submission.NameonCard" type="text" name="name3" ng-required="true"><br>
            <p class="error" ng-show="errorNameonCard">{{errorNameonCard}}</p>
            <br><label>CVD</label><input ng-model="submission.CVD" type="password" name="cvd" min="3" maxlength="3" ng-minlength="3" ng-maxlength="3" ng-required="true"><br>
            <p class="error" ng-show="errorCVD">{{errorCVD}}</p>

            <br><label>Expiry Month/Year</label><input ng-model="submission.Expiry" type="date" name="expire" ng-required="true"><br>
            <p class="error" ng-show="errorExpiry">{{errorExpiry}}</p>
            <br>
            <input ng-disabled="form1.$invalid" type="submit" value="Process payment" class="submission" ng-click="submit()"><br>

        </form>
    </div>

</body>

</html>
<?php
$conn= mysqli_connect("localhost","root","","db1");
$info = json_decode(file_get_contents("php://input"));
$data = array();
$error = array();

if (empty($info->Order)){
    $error["Order"] = "Order require";
}
if (empty($info->Ordertotal)){
    $error["Ordertotal"] = "Add the ordertotal";
}
if (empty($info->Cardtype)){
    $error["Cardtype"] = "Enter the name of the card";
}
if(empty($info->CreditCard)){
    $error["CreditCard"] = "Enter the card Number";
}
if(empty($info->NameonCard)){
    $error["NameonCard"] = "Enter Name of the Card";
}
if(empty($info->CVD)){
    $error["CVD"] = "Enter CVD";
}
if(empty($info->Expiry)){
    $error["Expiry"] = "Expiry date";
}
if(!empty($error)){
    $data["error"] = $error;
}
else
{
mysqli_real_escape_string($conn,$info->Order);
mysqli_real_escape_string($conn,$info->Ordertotal);
mysqli_real_escape_string($conn,$info->Cardtype);
mysqli_real_escape_string($conn,$info->CreditCard);
mysqli_real_escape_string($conn,$info->NameonCard);
mysqli_real_escape_string($conn,$info->CVD);
mysqli_real_escape_string($conn,$info->Expiry);

$query = "INSERT INTO db1_moneris customer data(Order,Ordertotal,Cardtype,CreditCard,NameonCard,CVD,expiry)
VALUES ('$Order','$Ordertotal','$Cardtype','$CreditCard','$NameonCard','$CVD','$Expiry',)
";
if(mysqli_query($conn,$query)){
    $data["message"] = "Data is in sql database";
}
}
echo json_encode($data);
?>

这是错误:

  

{&#34;错误&#34;:{&#34;订单&#34;:&#34;订单需要&#34;,&#34; Ordertotal&#34;:&#34;添加ordertotal&#34; 34;,&#34; Cardtype&#34;:&#34;输入卡的名称&#34;,&#34; CreditCard&#34;:&#34;输入卡号&#34;,&#34; ; NameonCard&#34;:&#34;输入卡的名称&#34;,&#34; CVD&#34;:&#34;进入CVD&#34;,&#34;到期&#34;:&#34; ;到期日期&#34;}}

0 个答案:

没有答案