I'm populating a table via a select
that I have in my file selectUser.php
.
selectUser.php:
<?php
require "conexaoBD.php";
$nome = $_GET["nome"];
try {
$pdo = new PDO($server, $dbuser, $dbpass);
$sql = "SELECT 'nome', 'email', 'nascimento', 'data_criacao' FROM 'usuarios' WHERE 'nome' like ? ";
$stmt = $pdo->prepare($sql);
$nome = $nome."%";
$stmt->bindParam(1, $nome, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $r) {
$nascimento = date('d/m/Y', strtotime(str_replace("-","/",$r["nascimento"])));
echo "<tr>
<td>$r[nome]<td/>
<td>$r[email]</td>
<td>$nascimento</td>
<td>$r[data_criacao]</td>
</tr>";
}
}
catch(PDOException $ex) {
echo "Error: ".$ex->getMessage();
}
?>
I make a get
via ajax
with the file main.js
to get the result.
main.js:
var buscaUser = function() {
var nome = $("#nome").val();
$.ajax({
url: "http://127.0.0.1:3000/projeto1/pdo/selectUser.php?nome=" + nome,
type: 'GET',
success: function(data) {
$("#corpo").append(data);
}
});
};
$('#buscar').click(function(e) {
e.preventDefault();
$('#corpo').children().remove();
buscaUser();
});
/*$("#nome").on('input', function(){
if($(this).val().length > 2 ){
buscaUser();
}
});*/
And in my file index.php
I have the table created as follows:
index.php:
<table class="table">
<thead>
<tr>
<th scope="col">Nome</th>
<th scope="col">E-mail</th>
<th scope="col">Data de Nascimento</th>
<th scope="col">Data de Criação</th>
</tr>
</thead>
<tbody id="corpo">
<tr>
<td>Nome</td>
<td>Email</td>
<td>Data nascimento</td>
<td>Data Criacao</td>
</tr>
</tbody>
</table>
Doing a test, looking for example by my name, I get the following result:
I have success in the query but an empty% void is generated, thus breaking the layout. Does anyone know where I'm going wrong?