How to organize DB information printed on the page

Asked

Viewed 26 times

0

I created a panel, where I want to show the information that is in db, these are printed on the screen through the code:

<?php
$servidor = "localhost";
$usuario = "root";
$senha = "";
$dbname = "id8146007_contasadm";

$conn = mysqli_connect ($servidor, $usuario, $senha, $dbname);
$db_select = mysqli_select_db ($conn ,$dbname);

$query = sprintf ("SELECT email, ddd, numero, FROM contas");
$dados = mysqli_query ($conn,$query);
$linha = mysqli_fetch_assoc ($dados);
$total = mysqli_num_rows($dados);
?>

So far ok, but the information just stays on the screen, how do I organize them? i wanted to put a button in front of each information that appears, button "delete", the same deletes the chosen information from DB. as I would do?

2 answers

1

You can create a stylized table with CSS. The result would be this:

inserir a descrição da imagem aqui

Now to delete the record, I recommend deleting by the user id. To do so, also select the id (if the column name is id) in consultation with the bank:

        ↓↓
"SELECT id, email, ddd, numero, FROM contas"

Codes:

CSS:

table{
   width: 100%;
   background-color: #ddd;
   border-spacing: 1px;
}

th, td{
   padding: 10px;
   text-align: center;
}

table th{
   background-color: #444;
   color: #fff;
}

table td{
   background-color: #fff;
}

table tfoot td{
   background-color: #f5f5f5;
}

table tfoot td:first-child{
   text-align: right;
}

HTML + PHP:

<?php
$servidor = "localhost";
$usuario = "root";
$senha = "";
$dbname = "id8146007_contasadm";

$conn = mysqli_connect ($servidor, $usuario, $senha, $dbname);
$db_select = mysqli_select_db ($conn ,$dbname);

$id = $_GET['id'];
if($_GET['acao'] == 'excluir' && !empty(id)){
   // exclui o registro do banco
   mysqli_query($conn, "DELETE FROM contas WHERE id = '$id'");
}

$query = sprintf ("SELECT id, email, ddd, numero, FROM contas");
$dados = mysqli_query ($conn,$query);
$linha = mysqli_fetch_assoc ($dados);
$total = mysqli_num_rows($dados);
?>
<table>
   <tr>
      <th>
         <strong>E-mail</strong>
      </th>
      <th>
         <strong>Número</strong>
      </th>
      <th>
      </th>
   </tr>
   <?php while($linha){ ?>
   <tr>
      <td>
         <?php echo $linha['email'] ?>
      </td>
      <td>
         (<?php echo $linha['ddd'] ?>) <?php echo $linha['numero'] ?>
      </td>
      <td>
         <button onclick='location.href = "?id=<?php echo $linha['id'] ?>&acao=excluir"'>Excluir</button>
      </td>
   </tr>
   <?php } ?>
   <tfoot>
      <td colspan="2">
         <strong>Total:</strong>
      </td>
      <td>
         <?php echo $total ?>
      </td>
   </tfoot>
</table>

0

You can do it like this:

if(isset($_GET['acao']) &&  $_GET['acao'] == 'excluir'){

$deletar = mysqli_query($conn,"delete from contas where email like '" . $_GET['email'] . "'");
}

$html = "<table><tr><td>Email</td><td>DDD</td><td>Conta</td><td></td></tr>";
while($res =  mysql_fetch_array( $dados )){

$html .= "<tr>
<td>" . $res['email'] . "</td>
<td>" . $res['ddd'] . "</td>
<td>" . $res['conta'] . "</td>
<td><a href=\"?acao=excluir&email=" . $res['email'] . "\">Excluir</a></td>
</tr>";

}

$html .= "</table>";

echo $html;

Browser other questions tagged

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