从Web应用程序发布到数据库失败

时间:2015-07-13 20:19:16

标签: javascript php mysql angularjs

在Web App中使用PHP和AngularJs的mysql数据库。 控制台没有抛出任何错误。我使用ng-modeldescpriccattitle双重绑定到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());

}

?>

我们已经使用带有JSON的Android平台上的PHP,它已经有效,它只是不适用于我们的有角度的网络应用程序。救命!谢谢! &lt; 3

2 个答案:

答案 0 :(得分:1)

您的代码中的一个问题是您尝试使用close() - 函数关闭PDO连接,这是不允许的。您应该将$ conn变量设置为null。请参阅此帖子:PDO closing connection

您可以通过将此添加到您的帖子请求中来查看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()in ...

当我从使用close() - 函数更改为将PDO连接设置为null时,您的代码适用于我(我一直在使用简单的虚拟数据),来自前端的数据将发布到数据库中。尝试在帖子请求函数中添加“console.log”,看看是否有任何错误可能会给你一些问题提示。

答案 1 :(得分:1)

在不知道任何错误消息或异常的情况下,我怀疑您遇到了CORS问题,您是否在http://speffs.one/SPE/post.php的响应中指定了Access-Control-Allow-Origin标头?

也不是你应该使用CORS预检POST / PUT / DELETE

请参阅: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests

当您使用cURL或任何其他HTTP库时,您不需要CORS,但如果您从浏览器发出请求,则需要它!