从web应用程序到数据库的发布失败
Posting from web app to database fails
在Web应用中使用mysql数据库与PHP和AngularJs。控制台没有抛出任何错误。我使用ng-model
双绑定desc
, pric
, cat
和title
在html中输入标签。这是我的javascript:
app.controller('MainController', ['$scope', '$http', function($scope, $http) {
$scope.light = "Light";
var _userId = 44;
$scope.desc = "" ;
$scope.pric = "" ;
$scope.cat = "";
$scope.title = "HERE I AM!";
$scope.sendPost = function() {
var data = $.param({
json: JSON.stringify({
userId: _userId,
price: $scope.pric,
description: $scope.desc,
category: $scope.cat,
postTitle: $scope.title,
photo1: "",
photo2: "",
photo3: "",
tag1: "",
tag2: "",
tag3: ""
})
});
$http.post("http://speffs.one/SPE/post.php", data).success(function(data, status) {
window.alert("HIII I AM IN");
});
};
}]);
一切都很好地编译/显示,但它不是张贴任何东西到数据库,当我做从数据库检索。成功提示"HI I AM IN"也正在执行。
下面是PHP:
<?php
//header('Access-Control-Allow-Origin: *');
//require_once 'https://filemanager.one.com/#aamirz@princeton.edu/speffs.one/files/SPE/connect.php';
$host = "";
$dbname = "";
$username = "";
$password = "";
// decode the json file
//$jsonInput = file_get_contents('http://speffs.one/SPE/test.txt');
$jsonInput = file_get_contents("php://input");
//$string = json_decode($jsonInput);
//$file = fopen("testing.txt", "w");
//$myfile = fopen("testfile.txt", "w");
//var_dump($_POST);
// $jsonInput = '{"user":"","price":"22.00","description":"dks","category":"ksks","photo1":"","photo2":"","photo3":"","tag1":"ks","tag2":"sksk","tag3":"skdkj","postTitle":"Testy "}';
//$string = "HIIIII!";
if(isset($jsonInput)) {
$JSON = json_decode($jsonInput, true);
//fwrite($myfile, $string);
}else{
$this->error = 'A valid JSON file was not specified';
}
// make a connection
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// $conn->(PDO::ATTR_EMULATE_PREPARES, false);
$sql = 'INSERT INTO Posts(userId,price,description,category,photo1,photo2,photo3,tag1,tag2,tag3,postTitle)
VALUES(:user,:price,:des,:cat,:p1,:p2,:p3,:t1,:t2,:t3,:title)';
// prepare the statement
$stmt = $conn->prepare($sql);
$time = date("Y-m-d h:i:s");
$stmt->bindParam(':cat', $JSON["category"], PDO::PARAM_STR);
$stmt->bindParam(':des', $JSON["description"], PDO::PARAM_STR);
$stmt->bindParam(':title', $JSON["postTitle"], PDO::PARAM_STR);
$stmt->bindParam(':price', $JSON["price"], PDO::PARAM_STR); // check the param of this
$stmt->bindParam(':user', $JSON["userId"], PDO::PARAM_STR); // or get from the system, $user = 'system';
$empty = '';
if ($JSON["photo1"] != null) {
$stmt->bindParam(':p1', $JSON["photo1"], PDO::PARAM_LOB);
} else {
$stmt->bindParam(':p1', $empty, PDO::PARAM_LOB);
}
if ($JSON["photo2"] != null) {
$stmt->bindParam(':p2', $JSON["photo2"], PDO::PARAM_LOB);
} else {
$stmt->bindParam(':p2', $empty, PDO::PARAM_LOB);
}
if ($JSON["photo3"] != null) {
$stmt->bindParam(':p3', $JSON["photo3"], PDO::PARAM_LOB);
} else {
$stmt->bindParam(':p3', $empty, PDO::PARAM_LOB);
}
if ($JSON["tag1"] != null) {
$stmt->bindParam(':t1', $JSON["tag1"], PDO::PARAM_LOB);
} else {
$stmt->bindParam(':t1', $empty, PDO::PARAM_LOB);
}
if ($JSON["tag2"] != null) {
$stmt->bindParam(':t2', $JSON["tag2"], PDO::PARAM_LOB);
} else {
$stmt->bindParam(':t3', $empty, PDO::PARAM_LOB);
}
if ($JSON["tag3"] != null) {
$stmt->bindParam(':t3', $JSON["tag3"], PDO::PARAM_LOB);
} else {
$stmt->bindParam(':t3', $empty, PDO::PARAM_LOB);
}
// $stmt->bindParam(':tim', $time, PDO::PARAM_STR); time was deleted, that's in mysql auto
$stmt->execute() ;
$stmt->close();
$conn->close();
} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
}
?>
我们已经在android平台上使用PHP和JSON,它已经工作了,只是它不适合我们的angular web应用程序。的帮助!谢谢你!& lt; 3
代码中的一个问题是,您试图使用close()-函数关闭PDO连接,这是不允许的。您应该将$conn变量设置为null。查看这个帖子:PDO关闭连接。
你可以通过在你的post请求中添加下面的代码来查看php代码中的错误:
$http.post("http://speffs.one/SPE/post.php", $scope.data).success(function(data, status) {
console.log("Data: "+data+" Status: "+status);
window.alert("HIII I AM IN");
});
然后它将在web浏览器控制台中打印错误。当运行你的代码时,我得到这个错误:致命错误:调用未定义的方法PDOStatement::close()在…
当我从使用close()函数更改为将PDO连接设置为null时,您的代码对我有效(我一直在使用简单的虚拟数据),来自前端的数据发布在数据库中。试着在你的post request函数中添加"console.log",看看你是否会得到任何可能给你提示问题所在的错误。
在不知道任何错误消息或抛出异常的情况下,我怀疑您正在遇到CORS问题,您是否在http://speffs.one/SPE/post.php上的响应中指定了Access-Control-Allow-Origin
标头?
也不是说你应该使用CORS preflight for POST/PUT/DELETE
看:https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS Preflighted_requests
当你使用cURL或任何其他HTTP库时,你不需要CORS,但如果你从浏览器发出请求,你将需要它!
- 当json解析空响应时,Whatwg-Fetch失败,我该如何防止它
- JsFiddle在分叉后描述失败
- 为什么不'当单元测试出现解析错误时,我的因果报应测试会失败
- 当一些承诺失败时,如何继续使用$q.all()
- 失败:等待Protractor与页面同步时出错:“”;在窗口上找不到角度”;
- 在Jquery中单击传播失败
- 网页上失败的javascript会导致所有其他脚本失败
- 带有对象解析的响应javascript ajax失败
- 一台特定计算机的Ajax请求数据未定义/失败
- Nodejs服务器:加载资源失败:服务器的响应状态为404(未找到)
- jQuery.getJSON失败,语法错误
- ng disabled在放入多个表达式时失败
- Jasmine单元测试在监视服务方法时失败
- jQuery分页下一页和上一页按钮在点击零或超过最后一页后失败
- 在iframe的情况下,jQuery html()将失败
- 在我的情况下,使用带有变量失败的 jquery 选择器
- CSS中的转换似乎失败了,原因是什么
- 在数组中插入对象失败
- Backbone fetch中的Ajax在fetch调用退出后完成,因此fetch调用中没有成功/失败事件
- 当其他解析器认为意外的令牌有效时,json.parse会失败