What’s wrong with my array in php?

Asked

Viewed 109 times

1

I am trying to assemble an array, in php, that return to my controller, because the return is showing on the 'Undefined' console, why?

Controller

.controller('usuarioCtrl', function ($scope, $http, $window, $location) {

$scope.salvaUsuario = function (usuario) {
    var idCep = $window.localStorage.getItem('idCep');
    usuario.idCep = idCep;

       $http.post("http://localhost:8888/sistemas/webApps/ionic/vcApp/www/php/salvaUsuario.php", usuario).success(function (data){

        console.log(data);
        var ema = $window.localStorage.setItem("emailLogin", data.email);
        //console.log(JSON.stringify(data));
        if(data.cod === 1){
            $location.path('/cadastraUsuario');
            $scope.msgExiste = "Usuário já existente. Tente outro.";
        }

        });
        $location.path('/page10');
    }
})

PHP

<?php
header('Content-Type: text/html; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type,X-Prototype-Version,X- Requested-With');

mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');

include_once("conPDO.php");

$pdo = conectar();

$data = file_get_contents("php://input");
$data = json_decode($data);

$nome = $data->nome;
$usuario = $data->usuario;
$email = $data->email;
$senha = $data->senha;
$idCep = $data->idCep;

$nome = utf8_decode($nome);
$tipoUsuario = "C";

$verificaUsuario=$pdo->prepare("SELECT * FROM usuarios WHERE nome=:nome AND email=:email");
$verificaUsuario->bindValue("nome", $nome); 
$verificaUsuario->bindValue("email", $email); 
$verificaUsuario->execute();

$quant = $verificaUsuario->rowCount();

if($quant != 1){

    $result = array([
       'email' => $email
    ]);

    $insereUsuario=$pdo->prepare("INSERT INTO usuarios (idUsuario, idCep, tipoUsuario, nome, usuario, email, senha) VALUES (?, ?, ?, ?, ?, ?, ?)");
    $insereUsuario->bindValue(1, NULL); 
    $insereUsuario->bindValue(2, $idCep); 
    $insereUsuario->bindValue(3, $tipoUsuario); 
    $insereUsuario->bindValue(4, $nome);
    $insereUsuario->bindValue(5, $usuario);
    $insereUsuario->bindValue(6, $email);
    $insereUsuario->bindValue(7, $senha);

    $insereUsuario->execute();

    echo json_encode($result[0]);

}else{

    $result = array(
       'cod' => 1,
      );

    echo json_encode($result);

    return false;

}
  • try to use without keys $result = array( 'email' => $email );

  • @Raphaelcaldas, what appeared on the console, now was: "<b>Notice</b>: Undefined offset: 0 in <b>/home/vigil465/public_html/www/php/salvaUsuario.php</b> on line <b>52</b><br /> null"

  • I did an answer explaining buddy, good luck on your project, from a look there, until more ;D

2 answers

1


Buddy I tested here on my website.

I’ve come to the conclusion that you should use ['CAMPO'].

Then it would be:

json_encode($result['email'])

Unfortunately I don’t know how to tell you why it should be used like this, but I’ll give you a guess, here it goes: as can be noticed, your array has values defined as if it were a Json (Example Comparison), so you should pull as if it were a Json, but then instead of using -> use [''].

I hope you can tidy up on your system, take a look at my site that is with the link below, there I show the steps I used to reach such conclusion.

Good luck with your project, anything I’m here to help ;D

As I show you here on my website: http://netescola.info/stackover/

  • I got it. I got it just like this json_encode($result). Thanks anyway.

1

Some problems I see in your code:

1 - If it is a json that you are returning in the reply, the Header must be compatible. Ex: Instead of header('Content-Type: text/html; charset=utf-8'); use header('Content-Type: application/json; charset=utf-8');

2 - No need to use:

$result = array([
   'email' => $email
]);

To then use echo json_encode($result[0]); You can do straight echo json_encode(array('email' => $email));

3 - And in Else when you return a "Cod=1", but at the angle you try to access the date.email before comparing Cod, so if you fall into this Else it will give a Undefined error because the date.email there is only the date.Cod. Or if you still want to resume the email as well, return echo echo json_encode(array('cod' => 1 , 'email' => $email));

4 - That false Return at the end is right there :p

  • Hi @Andrékiffer Regarding your comment number 3, it will only resume Cod=1 if it falls into the existing user condition. Another thing I’ve noticed is that if I create this array, it might not be necessary to give an echo json_encode($result[0]). Thanks, you helped me finish this kkkkkk

Browser other questions tagged

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