(PHP) Get repeated data (MySQL) from a value in a dropdown list and show it in a table

957

I have a file that has a dropdown list with a set of values (matricula) from a database (movimentos) what I want is when I select that value in the dropdown list it searches the database for repeated rows and it shows it in a table with all the data from that value (it's currently 3 columns, "matricula,"marca" and "despesa")

Below is the code I use to get the value from the dropdown list, and this is where I have the problem, I can't get the value from the dropdown list to show the respective rows in the table.

 <a href='verificar.dwt.php'>Voltar atrĂ¡s</a>

<div align="center"><? 

include 'configmov.dwt.php'; 

$tableName='movimentos';
$matricula = mysql_real_escape_string($_POST['matricula']);

$sql="SELECT matricula, marca, despesa FROM $tableName WHERE $matricula in (SELECT matricula FROM $tableName GROUP BY $matricula HAVING count($matricula) > 1)"; 
$result=mysql_query($sql); 
$n=1; 

echo "Os seus resultados: <p>";

echo "<table border=0>";
echo "<tr bgcolor='#CCFFCC'>";
echo "<td style='width: 100px;'>Matricula</td>";
echo "<td style='width: 100px;'>Marca</td>";
echo "<td style='width: 100px;'>Despesa</td>";
echo "</tr>";

while($row = mysql_fetch_array($result)){ 
    echo "<table border=0>";
    echo "<tr bgcolor='#CCCCCC'>";
    echo "<td style='width: 100px;'>".$row['matricula']."</td>";
    echo "<td style='width: 100px;'>".$row['marca']."</td>";
    echo "<td style='width: 100px;'>".$row['despesa']."</td>";
    echo "</tr>";

}

?>

</div>

<?php
// close connection; 
mysql_close();
?>

I'm around this problem for a while and I did a lot of research before coming here to make this question.

219

Answer

Solution:

matricula is a field from table, in your post... not a php variable..So you must have : GROUP BY matricula HAVIG count(matricula)

765

Answer

Solution:

Please make sure you SQL syntax is correct and try this code for making a table from returned database values to make it a single table

echo "<table border=0>";
echo "<tr bgcolor='#CCFFCC'>";
echo "<td style='width: 100px;'>Matricula</td>";
echo "<td style='width: 100px;'>Marca</td>";
echo "<td style='width: 100px;'>Despesa</td>";
echo "</tr>";

while($row = mysql_fetch_array($result)){ 
    echo "<tr bgcolor='#CCCCCC'>";
    echo "<td style='width: 100px;'>".$row['matricula']."</td>";
    echo "<td style='width: 100px;'>".$row['marca']."</td>";
    echo "<td style='width: 100px;'>".$row['despesa']."</td>";
    echo "</tr>";

}

echo "</table>";
694

Answer

Solution:

First of all.. I think that your syntax is wrong.

Try this one:

$sql = "SELECT matricula, marca, despesa FROM '$tableName' WHERE '$matricula' in (SELECT matricula FROM '$tableName' GROUP BY '$matricula' HAVING count('$matricula') > 1)"; 

In a query you must put the php variables between quotes.

364

Answer

Solution:

if you are checking for an exact match in Table for the posted value, why cant you use this instead?

 $sql="SELECT matricula, marca, despesa FROM ".$tableName." WHERE matricula = '".$matricula."'"; 

People are also looking for solutions to the problem: Fetching a random row from MySQL and displaying it with PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.