Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从web应用发布到数据库失败_Javascript_Php_Mysql_Angularjs - Fatal编程技术网

Javascript 从web应用发布到数据库失败

Javascript 从web应用发布到数据库失败,javascript,php,mysql,angularjs,Javascript,Php,Mysql,Angularjs,在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 =

在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");
    });
}; 


}]);
一切都很好地编译/显示,但当我从数据库中检索时,它没有将任何内容发布到数据库中。成功警报“你好,我在”也正在执行

以下是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());

}

?>

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

通过将以下内容添加到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请求函数中添加“console.log”,看看您是否收到任何可能提示问题所在的错误。

在不知道引发的任何错误消息或异常的情况下,我怀疑您遇到了CORS问题,您是否在上的响应中指定了
访问控制允许来源

也不是说你应该使用CORS飞行前的POST/PUT/DELETE

见:


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

如果警报正在运行,那么问题出在PHP中。您可能希望向他人展示您的PHP代码以提供帮助。如果您在任何地方都没有看到任何错误,我怀疑自动提交已被关闭。因此,由于您没有明确地告诉它提交,所以它只是在您关闭连接时转储您的输入。但看起来您并没有在代码中设置它。谢谢!我们之前已经遇到了CORS问题,并添加了插件。然而,这不是这次的错误。这是一个非常有用的评论!我们修复了它,现在发布了它,但是我们不断收到500个内部服务器错误,我们已经阅读了超过1000次php。这是可行的,但是这个错误让我们感到紧张-你知道它的意思吗?这些链接可能很有趣:而且我们每次插入帖子都会遇到这个错误。问题是我们的PHPWeb服务不是本地托管的,我们是从one.com托管的,它不断地抛出这个错误。似乎你提供的链接考虑了本地配置。谢谢你!