How to organize DB information printed on the page

0

I created a panel, where I want to show the information that is in the 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 so good, but the information just stays on the screen, how do I organize it? I wanted by a button to the front of each information that appears, button "delete", it excludes the information chosen from the DB. how would I do it?

    
asked by anonymous 18.12.2018 / 03:58

2 answers

1

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

Nowtodeletetherecord,Irecommenddeletingitbyuserid.Todothis,alsoselecttheid(ifthecolumnnameisid)inthedatabasequery:

↓↓"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>
    
18.12.2018 / 16:15
0

You can do 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;
    
18.12.2018 / 12:11