File IMG_TROCAR.PHP code
<?php
include_once('config/config.php');
$ligacao = new PDO("mysql:dbname=$baseDado;host=$host", $user, $pass);
$id = $_POST['id_user'];
$mudarAvatar = $_FILES['img_avatar'];
if($mudarAvatar['name'] == ""){
//header('Location:perfil.php?ref=configuracao');
echo '<p>avatar esta fazio</p>';
exit;
}else if($mudarAvatar['name']){
//faz a troca de foto do avatar no banco de dado
$atualizarAvatar = $ligacao->prepare("UPDATE users SET avatar = ? WHERE id_user = ?");
$atualizarAvatar->bindParam(1, $mudarAvatar['name'], PDO::PARAM_STR);
$atualizarAvatar->bindParam(2, $id, PDO::PARAM_INT);
$atualizarAvatar->execute();
$location = "avatar/";
move_uploaded_file($mudarAvatar['tmp_name'], $location.$mudarAvatar['name']);
//header('Location:perfil.php?ref=configuracao');
}
//busca a informação do usuario
$sql = "SELECT avatar, username, email_user FROM users WHERE id_user = :id";
$consulta = $ligacao->prepare($sql);
$consulta->bindParam(':id', $id, PDO::PARAM_INT);
$consulta->execute();
$result[] = $consulta->fetch(PDO::FETCH_ASSOC);
echo json_encode($result, JSON_PRETTY_PRINT);
$ligacao = null;
?>
I have a JSON file img_trocar.php that returns only a single line of a contact
[
{
"avatar": "IMG_20180208_171358_956.jpg",
"username": "rafaelshembek",
"email_user": "[email protected]"
}
]
So far so good. Now the problem is here in that code,
function uploadFoto(){
var img = '';
var location = $('.r_f_p');//local onde vai aparece as informações do usuario
$.getJSON('img_trocar.php')
var obj = JSON.parse(dado)
.always(function(dado){
img += '<p>' + dado[0].username + '</p>';
img += '<img src="avatar/'+dado[0].avatar+'">';
});
location.html(img);
}
The problem is that the information on the page does not appear for the user in the console, it says that the data is not defined, someone has a solution for this.