【发布时间】:2016-09-26 06:41:13
【问题描述】:
发生了一些奇怪的事情,我将插入数据从角度控制器传递到 php 文件。接下来,php 文件将数据插入 MySQL 数据库。所有数据都成功插入。但是,一旦我单击按钮,两条记录就会插入 MySQL 数据库。一是插入空数据,二是我从 UI 输入的数据。以下是我的代码:
Javascript 控制器代码:
.controller('registerController',['$scope', '$stateParams', '$state', '$http', function($scope, $stateParams, $state, $http){
var userInformation = [];
$scope.registration = {};
console.log($stateParams.all);
$scope.doRegister = function () {
if( $scope.registration.pasword !== $scope.registration.confirmpassword){
alert("Password not match");
}
$http({
url: "http://localhost/php/chat.php",
method: "POST",
data: {
'username': $scope.registration.username,
'email': $scope.registration.emailaddress,
'password': $scope.registration.pasword
}
}).success(function(response) {
console.log(response);
}).error(function(response) {
console.log(response);
});
$state.go('login', {userInformation : userInformation});
};
$scope.goBack = function() {
$state.go('login');
}
}])
PHP 代码
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$user = $request->username;
$email = $request->email;
$pass = $request->password;
$servername = "localhost";
$username = "jack";
$password = "1234";
$dbname = "chat";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO use_directory VALUES ('', '$user', '$email', '$pass');" ;
$retval = $conn->multi_query($sql);
if( $retval === TRUE) {
echo "Add successfully\n";
}else {
die('Could not edit data');
}
?>
这是我的数据库结果
【问题讨论】:
-
y 使用
multi_query? -
@dass 我从互联网上复制的这段代码。启用将数据插入数据库
-
将
$conn->multi_query($sql)更改为$conn->query($sql) -
@dass 插入两条记录仍然相同,其中一条记录数据为空,另一条记录数据我输入。我认为不是PHP的问题。
标签: javascript php mysql angularjs ionic-framework