Hi I am having some trouble with a small project. It's my first time using mySQL and PHP so I'm just trying to get connectivity for now. I have followed some tutorials and I cannot connect to the database, I am getting the "Sorry! Data Couldnt be inserted!" error from my php script at the bottom of index.html.
I also have a password on the database for the root user however even when i type this in the php file it still wont work.
Any help is much appreciated guys.
index.html:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"></link>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body>
<div class= "container" style = "width:500px">
<div ng-app="myapp" ng-controller="usercontroller">
<label>Email</label>
<input type ="text" name="email" ng-model="email" class="form-control"/>
<br />
<label>Password</label>
<input type ="text" name="password" ng-model="password" class="form-control"/>
<br />
<input type="submit" name="btnInsert" ng-click="insertData()" value="ADD"/>
</div>
</div>
<script>
var app = angular.module('myapp',[]);
app.controller('usercontroller', function($scope, $http){
$scope.insertData = function(){
$http.post(
"insert.php",{
'email':$scope.email,
'password':$scope.password
}).then(function(response){
alert("Data inserted Successfully! YIPEEEE!!!");
console.log("Data inserted Successfully");
},function(error){
alert("Sorry! Data Couldnt be inserted!");
console.error(error);
});
}
});
</script>
</body>
</html>
and my insert.php:
<?php
//insert.php
$data = json_decode(file_get_contents("php://input"));
$email = $data->email;
$password = $data->password;
mysql_connect("localhost","root","");
mysql_select_db("test");
mysql_query ("INSERT INTO 'test_user'('email', 'password') VALUES ('".$email."','".$password."')") or die(mysql_error());
echo $email. " " .$password;
/*
if(mysqli_query($connect, $query)){
echo "User inserted...";
}
else{
echo "User not inserted...";
}
*/
#mysqli_real_escape_string($connect,
#mysqli_real_escape_string($connect,
?>
</div>
It's my first time using mySQL and PHP
But you seem to be using a very old version of PHP (the mysql_ extension is deprecated).
I'm not familiar with Angular but it looks like your code relies on Angular to determine if the PHP script ran as intended. Angular can't base that decision on the content sent by your script, therefore it must be basing its decision on the HTTP status returned by your script (which has no debugging in it). I expecting your PHP code is crashing and burning before it gets to run the insert.
I suggest you:
1) build a test rig using a textare and NO javascript to fire a request at your insert.php script
2) amend your PHP script so that it - captures more information about the processing - uses appropriate error reporting and ensure your error logging is working - presents the (appropriate) details of the actions it took in a parseable format (i.e. JSON)
3) read your log files - for both PHP and MySQL (you might want to do this before going to the trouble of changing/verifying your logging as described above)
4) read up on SQL injection