3

Using mysql database with PHP and AngularJs in Web App. The console is not throwing any errors. I used ng-model to double bind desc, pric, cat, and title to input tags in the html. Here is my 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");
    });
}; 


}]);

Everything is compiling/displaying nicely but it is not posting anything to the database when I do a retrieval from the database. The success alert "HI I AM IN" is also executing.

Here is the 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());

}

?>

We've used PHP from an android platform with JSON and it has worked, it's just not working for our web app with angular. Help! Thank you! <3

  • If the alert is getting ran, then your problem is within PHP. You may want to show your PHP code for someone to help. – KJ Price Jul 13 '15 at 20:21
  • If you're not seeing any errors anywhere, I'd suspect that auto commit has been turned off somehow. So, as you are not explicitly telling it to commit, it's just dumping your input when you close the connection. But it doesn't look like you are setting it in your code. – CargoMeister Jul 13 '15 at 22:15

2 Answers2

1

One problem in your code is that you are trying to close the PDO connection using the close()-function, and this is not allowed. You should instead set the $conn variable to null. See this post: PDO closing connection.

You can see errors in the php code by adding this to your post request:

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

It will then print the errors in your web browser console. When running your code I get this error: Fatal error: Call to undefined method PDOStatement::close() in...

When I changed from using the close()-function to setting the PDO connection to null your code works for me (I have been using simple dummy data though), the data from the frontend is posted in the database. Try adding the "console.log" in your post request function and see if you get any errors that might give you a hint of what the problem is.

Community
  • 1
  • 1
Fredrik Schöld
  • 1,588
  • 13
  • 20
  • We fixed it and it is posting now however we keep getting a 500 internal server error and we have read the php over a thousand times. It works, but the error is making us nervous- do you know what it means? – Aamir Zainulabadeen Jul 14 '15 at 18:48
  • These links might be interesting: http://stackoverflow.com/questions/17693391/500-internal-server-error-for-php-file-not-for-html and http://stackoverflow.com/questions/11179706/getting-internal-server-error-while-trying-to-access-my-site and http://stackoverflow.com/questions/17050164/jquery-php-post-error-500-server-internal-error – Fredrik Schöld Jul 14 '15 at 19:51
  • We get this error every time we insert a post. The thing is that our php web service is not hosted locally, we are hosting off of one.com, and it is throwing this error constantly. It seems like the links you provide consider local configurations. Thank you though! – Aamir Zainulabadeen Jul 15 '15 at 21:10
1

Without knowing any error messages or exceptions thrown I suspect you are running into CORS issues, have you specified a Access-Control-Allow-Origin header in the response on http://speffs.one/SPE/post.php ?

Also not that you should use CORS preflight for POST/PUT/DELETE

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

When you use cURL or any other HTTP library you don't need CORS, but if you make requests from a browser you will need it!

EJTH
  • 2,178
  • 1
  • 20
  • 25
  • Thank you! We already ran into the CORS issue earlier and added the plugin. However, that is not the bug this time. This is a very helpful comment though! – Aamir Zainulabadeen Jul 14 '15 at 14:04