Table populated via bank coming with empty field

Asked

Viewed 84 times

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: inserir a descrição da imagem aqui

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

  • 1

    What a mistake nonsense! Thank you @White, that’s right ;D

  • Go on, go on :)

  • 1

    Put it as an answer so I can mark it right.

  • 1

    Typing error is a bar!!! ) best is to remove the question.

  • It’s already xD you might not call and leave it as certain :)

  • @Jorgematheus the king of PHP !! :)

Show 2 more comments

1 answer

2


The tag <td> is badly closed in selectUser.php. Copy this into your code:

<tr>
<td>$r[nome]</td>
<td>$r[email]</td>
<td>$nascimento</td>
<td>$r[data_criacao]</td>
</tr>

Browser other questions tagged

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