So, I'm new to AngularJS and I was trying to do a simple web page to add values to a MySQL database with PHP. Here is the HTML:
<DOCTYPE html>
<html lang="pt">
<head>
<meta name="viewport" content="width=device-width">
<meta charset="utf-8">
<script src="js/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<script src="js/pannel.js"></script>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css"/>
<link rel="stylesheet" href="css/pannel.css" type="text/css"/>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="index.html">Garagi Extreme</a>
</div>
<div>
<ul class="nav navbar-nav">
<li class="active">
<a href="#">Inserir Carro</a>
</li>
<li><a href="#">Alterar/excluir Carro</a></li>
<li><a href="#">Inserir Montadora</a></li>
<li><a href="#">Alterar/excluir Montadora</a></li>
</ul>
</div>
</div>
</nav>
<div ng-app="garagem" ng-controller="montadora">
<form>
<p>Nome da Montadora: <input type="text" ng-model="nomeMont"/></p>
<input type="button" ng-click="insertMontadora()" value="Enviar" class="btn primary-btn"/>
</form>
</div>
</body>
</html>
And My pannel.js:
var app= angular.module('garagem', []);
app.controller('montadora',["$scope", "$http", function ($scope, $http) {
$scope.insertMontadora=function () {
$http.post("db/insertMont.php", {'nomeMont': $scope.nomeMont})
.success(function (data,status,headers,config) {
console.log("Inserted");
});
}
}]);
And my PHP page to connect to the database and make the query:
$nomeMont=mysql_real_escape_string($data->nomeMont);
$local_server="localhost";
$usuario= "tkc";
$senha="185478";
$banco="garagiextrem";
mysql_connect($local_server, $usuario, $senha) or die("Nope");
mysql_select_db($banco) or die("Nain");
mysql_query('INSERT INTO montadora(idMontadora,Nome) VALUES ("NULL",'" . $nomeMont . "')');
?>
When I load the page on a browser, everything works just fine, and I even get the "Inserted" message on the console. But when I go check the phpMyAdmin, there is no entries on the table. What am I doing wrong here? :(
mysql_query
(or other function calls). Try checking for errors rather than assuming everything just works. Also, you should usemysqli
sincemysql
is depreciated and will be removed from future releases of php. – Tristan Dec 6 '15 at 1:12