Difficulty updating data with angular and php

Asked

Viewed 281 times

3

Good afternoon

I’m trying to update the database and I can’t. If I use mysqli, the following warning appears on the console: "http://localhost:8888/systems/systems_web/Vigilantescomunitarios/admin/php/updateUsuario.php 500 (Internal Server Error)"

If I use mysql and PDO connection, nothing happens, nor notice on the console appears.

My js file:

app.controller("AtualizarUsuarioController", function ($scope, $window, $http, $location) {

$scope.usuario = {
    'id': $window.localStorage.getItem('idUsuarios'),
    'nome': $window.localStorage.getItem('nome'),
    'email': $window.localStorage.getItem('email')
}

//$location.path('/atualizarUsuario' + $scope.usuario.id);
$scope.atualizarUsuario = function (usuario) {
    $http.post("admin/php/atualizarUsuario.php", usuario).then(function (data){
        $location.path("#/usuarios");
    });
};
});

php:

<?php
header("Access-Control-Allow-Origin: *");

include_once ("conPDO.php");
$pdo = conectar();

$postdata = file_get_contents("php://input");
$data = json_decode($postdata);
$id = $data->id;
$nome = $data->nome;
$email = $data->email;
$senha = $data->senha;

$senha = sha1($senha);

$usuarioAtual=$pdo->prepare("UPDATE usuarios SET nome=:nome, email=:email, senha=:senha WHERE id=:id");
$usuarioAtual->bindValue(":nome", $nome);
$usuarioAtual->bindValue(":email", $email);
$usuarioAtual->bindValue(":senha", $senha);
$usuarioAtual->bindValue(":id", $id);
$usuarioAtual->execute();

And my folder structure. Pastas

Console return after print_r and console.log Dados no console

  • Already checked if the URL of the Api is correct, I do not know php but I would put a print in the method that updates to see if it is being called

  • Yes, the url is correct. I use the Postman, Chrome application, to check this and pass data.

  • You could post the code of the files "con.php" and "conPDO.php"?

  • @Gustavosevero Internal error means there was a server crash. Go to the Networks tab of the browser and check the file updateUsuario.php that will have the server’s answer containing the error. If not, check the server logs.

1 answer

1


Your problem may be in the method used in $http, in your case you are using PUT. In the past I had problems using it also because there are settings and (some cases) server limitations that prevent the PUT be called.

Use POST, out some particularities - which will not imply in your case- you will have no problem and may be able to solve.

$http.post([url], [data], [config]);

Note: Do not use print screen of your code, use the same code.


Edited:

Another important point would be to use print_r (in php) and console.log (at Angular) to better debug what is happening and where the problem is.

//No AngularJs
$scope.atualizarUsuario = function (usuario) {
    $http.post("admin/php/atualizarUsuario.php", usuario).then(function (data){
        console.log(data); //'data' pois é o valor definido dentro do `function` do `then`
        $location.path("#/usuarios");
    });
};

//E no PHP
$usuarioAtual->execute();
print_r($usuarioAtual);

This way you have a better view of what is happening.

  • I made Celso changes. I changed from put to post, tb... But still, nothing, not updating

  • of a print_r($usuarioAtual) and a console.log(data) inside the . then of $http to see what php returns. Always try to fetch the logs. No error will appear as you are not asking to display anything, either in php or at the angle.

  • print_r($data) at the end of my php code?

  • It can be yes, and you can use other prints, such as $Postdata to confirm that the data is actually coming.

  • There is some undefined variable on line 14 of your file. Search for it and see if it has actually been defined.

  • Sorry, but what do you mean? It’s all right. I was giving error because I had put $print_r, then corrected for print_r

  • Celso, I put, here in the post, a return print on the console.

  • Okay, the date is coming up correctly, but what is the return of $usuarioAtual, that is, the process of updating the data in the database?

  • It’s killing me... and I’ve seen where it is.

  • Now I fixed and worked Celso. I was putting the wrong id in SQL. Thanks for giving me this print_r tip, because I didn’t know I could see it using: "$Postdata = file_get_contents("php://input"); $data = json_decode($Postdata);" E tb didn’t know I could use at the end of the code, tb, to see what it showed up. Using PDO, it is more complicated to debug SQL, it is not?

  • 1

    Yes, it is, but I always use this same process of giving a print_r in . php and console.log in . js, so I can always debug what’s going on. Often they are silly mistakes that we do not realize. But good that helped! = D

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.