我正在尝试通过表单向我的数据库添加一些数据。但我在控制台中遇到错误
"Undefined property: stdClass::$name in ..."
(此错误发生在create.php
第6行。)我尝试从(下图中)读取数据的每一行都收到此错误。我无法理解为什么它不起作用。
控制台错误消息
create.php
<?php
include '../src/config/db2.php';
$data = json_decode(file_get_contents("php://input"));
$advertiser_name = $data->name;
$advertiser_org = $data->orgnumber;
$advertiser_phone = $data->phone;
$advertiser_address = $data->address;
$advertiser_postcode = $data->postcode;
$advertiser_location = $data->location;
$advertiser_billAddress = $data->billAddress;
$advertiser_billPostcode = $data->billPostcode;
$advertiser_billLocation = $data->billLocation;
$sql = "INSERT INTO `tbl_advertisers` (
`advertiser_name`,
`advertiser_org`,
`advertiser_phone`,
`advertiser_address`,
`advertiser_postcode`,
`advertiser_location`,
`advertiser_billAddress`,
`advertiser_billPostcode`,
`advertiser_billLocation`)
VALUES (
'$advertiser_name',
'$advertiser_org',
'$advertiser_phone',
'$advertiser_address',
'$advertiser_postcode',
'$advertiser_location',
'$advertiser_billAddress',
'$advertiser_billPostcode',
'$advertiser_billLocation')";
if (mysqli_query($conn, $sql)) {
echo "Data added";
}
else {
echo "Error: Did not add data $sql" .mysqli_error($conn);
}
mysqli_close($conn);
?>
HTML
<div class="container wrapper">
<br/>
<br/>
<br/>
<h3 class="">Add Company Information</h3>
<div class="table-responsive">
<table class="table table-hover">
<form >
<div ng-show="msg">
<strong>Success!</strong> Company Added!.
</div>
<div class="form-group">
<label for="advertiser_name">Company name</label>
<input class="form-control" type="text" name="advertiser_name" ng-model="advertiser_name" />
</div>
<div >
<label for="advertiser_org">Company number</label>
<input class="form-control" type="text" name="advertiser_org" ng-model="advertiser_org" />
</div>
<div >
<label for="advertiser_phone">Phone</label>
<input class="form-control" type="text" name="advertiser_phone" ng-model="advertiser_phone" />
</div>
<div >
<label for="advertiser_address">Address</label>
<input class="form-control" type="text" name="advertiser_address" ng-model="advertiser_address" />
</div>
<div >
<label for="advertiser_postcode">Postcode</label>
<input class="form-control" type="text" name="advertiser_postcode" ng-model="advertiser_postcode" />
</div>
<div >
<label for="advertiser_location">Post Area</label>
<input class="form-control" type="text" name="advertiser_location" ng-model="advertiser_location" />
</div>
<br/>
<h3>Billing Information</h3>
<div >
<label for="advertiser_billAddress">Address</label>
<input class="form-control" type="text" name="advertiser_billAddress" ng-model="advertiser_billAddress" />
</div>
<div >
<label for="advertiser_billPostcode">Postcode</label>
<input class="form-control" type="text" name="advertiser_billPostcode" ng-model="advertiser_billPostcode" />
</div>
<div >
<label for="advertiser_billLocation">Post Area</label>
<input class="form-control" type="text" name="advertiser_billLocation" ng-model="advertiser_billLocation" />
</div>
<br/>
<button type="submit" class="btn btn-primary" ng-click="add_company()">Submit</button>
<a href="index.html" class="btn btn-warning" >Go Back</a>
</form>
</table>
</div>
</div>
Playercontroller.js
var app = angular.module('crudApp', ['ngRoute']);
app.controller('mainController', function($scope, $http, $timeout,
$location) {
$scope.add_company = function(){
$scope.msg=false;
//set the data array
console.log($scope.msg);
//use $http.post to send above data to php
$http.post('php/create.php', {
'name':$scope.advertiser_name,
'orgnumber':$scope.advertiser_org,
'phone':$scope.advertiser_phone,
'address':$scope.advertiser_address,
'postcode':$scope.advertiser_postcode,
'location':$scope.advertiser_location,
'billAddress':$scope.advertiser_billAddress,
'billPostcode':$scope.advertiser_billPostcode,
'billLocation':$scope.advertiser_billLocation
}).then(function(response){
//clear the form fields
$scope.msg = true;
$timeout(function(){$scope.msg = false;}, 1000);
console.log(response.data);
});
console.log($scope.advertiser_name);
};
});