Take multiple rows from a table in the database with the same name and display the name only once in a list

Asked

Viewed 51 times

0

My form has the option to add more rows, so I had to use array and send its information to the database. In this case the database has created 3 rows within the table with various information, but the name (column in specific) has the same information in the three, and at the time of calling for html I would like to call only once this name that repeats 3 times.

connection to db:

<?php
require_once('db_connect.php');

for( $i=0; $i<count($_POST['exercicio']); $i++ ) {     

    // INSERINDO NO MYSQL
    $sql = "INSERT INTO ficha  
                (nomeFicha,
                nomeExercicio,
                grupoMuscular, 
                numSeries,
                numRepeticoes,
                peso   
                ) 
            VALUES 
                (
                '".$_POST['nomeFicha']."',
                '".$_POST['exercicio'][$i]."',             
                '".$_POST['grupoMusc'][$i]."',
                '".$_POST['numSeries'][$i]."',
                '".$_POST['numRep'][$i]."',
                '".$_POST['peso'][$i]."'  
                )";

                echo $sql;

$queryExec = mysqli_query($connect, $sql) or die('ERRO ao inserir registro no Banco');
}

    if ( $queryExec) {
        header('location: ../fichas.php?sucesso');     
    } else {        
        header('location: ../fichas.php?erro'); 
    }

html:

 <div id="divContent" class="container-fluid">
          <ul id="lista" class="list-group">
            <?php
              $sql ="SELECT nomeFicha FROM ficha";
              $resultado = mysqli_query($connect, $sql);
              while($dados = mysqli_fetch_array($resultado)):
            ?>

              <li class="list-group-item"><?php echo $dados['nomeFicha']; ?></li>

            <?php endwhile; ?>
          </ul>

1 answer

2


You can use the instruction DISTINCT to return only the different elements in your table. Your appointment would look like this:

SELECT DISTINCT nomeFicha FROM ficha;

https://www.w3schools.com/sql/sql_distinct.asp has more information on this.

  • I had already managed to find the answer but vlw was this msm that I wanted. I’ll look at the link that you sent.

  • @Lucaskohler be careful with this reference. w3schools is not reliable.

Browser other questions tagged

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