2
I’m populating a table via a select
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 for example by my name, I get the following result:
I have success in the consultation but is generated a <td>
empty thus breaking the layout.
Does anyone know where I’m going wrong?
You have badly closed td -> "<td>$r[name]<td/>" to: "<td>$r[name]</td>" in the selectUser.php file
– White
What a mistake nonsense! Thank you @White, that’s right ;D
– Jorge.M
Go on, go on :)
– White
Put it as an answer so I can mark it right.
– Jorge.M
Typing error is a bar!!! ) best is to remove the question.
– user60252
It’s already xD you might not call and leave it as certain :)
– White
@Jorgematheus the king of PHP !! :)
– White