I am trying to perform insert operation using Angular JS and mysql but it is working. Please check the below code and help me to find out the issues.
Following is the code of my index.html file
<html ng-app="myapp">
<head>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<script src="js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
</head>
<body ng-controller="formctrl" novalidate>
<form ng-submit="submitStudentForm()">
<input type="text" name="fname" ng-model="fname" placeholder="First Name"><br>
<input type="text" name="lname" ng-model="lname" placeholder="Last Name"><br>
<input type="submit" value="Submit" name="submit"/>
</form>
<script>
var app = angular.module('myapp', []);
app.controller('formctrl', function ($scope, $http) {
$scope.submitStudentForm = function () {
$http.post("add.php");
};
}
);
</script>
</body>
</html>
Following is the code of add.php
<?php
$conn = new mysqli("localhost", "root", "root", "test2");
if (isset($_POST['submit'])) {
$submit = $_POST['submit'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$sql = "INSERT into info(fname, lname)
VALUES ('$fname', '$lname')";
$result = mysqli_query($conn, $sql);
if ($result) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>
I bet u have no data to post on your http post. try editing on your http post:
$http.post("add.php", {
'fname':$scope.fname,
'lname':$scope.lname})