javascript - Posting from web app to database fails -
using mysql database php , angularjs in web app. console not throwing errors. used ng-model
double bind desc
, pric
, cat
, , title
input tags in html. here javascript:
app.controller('maincontroller', ['$scope', '$http', function($scope, $http) { $scope.light = "light"; var _userid = 44; $scope.desc = "" ; $scope.pric = "" ; $scope.cat = ""; $scope.title = "here 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 in"); }); }; }]);
everything compiling/displaying nicely not posting database when retrieval database. success alert "hi in" executing.
here 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 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 not specified'; } // make 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 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 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 param of $stmt->bindparam(':user', $json["userid"], pdo::param_str); // or 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 deleted, that's in mysql auto $stmt->execute() ; $stmt->close(); $conn->close(); } catch (pdoexception $pe) { die("could not connect database $dbname :" . $pe->getmessage()); } ?>
we've used php android platform json , has worked, it's not working our web app angular. help! thank you! <3
one problem in code trying close pdo connection using close()-function, , not allowed. should instead set $conn variable null. see post: pdo closing connection.
you can see errors in php code adding 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 in"); });
it print errors in web browser console. when running code error: fatal error: call undefined method pdostatement::close() in...
when changed using close()-function setting pdo connection null code works me (i have been using simple dummy data though), data frontend posted in database. try adding "console.log" in post request function , see if errors might give hint of problem is.
Comments
Post a Comment