繁体   English   中英

(PHP)从下拉列表中的值获取重复数据(MySQL)并将其显示在表中

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

我有一个文件,其中包含一个下拉列表,其中包含一组来自数据库的值(matricula)(movimentos)我想要的是当我在下拉列表中选择该值时,它会在数据库中搜索重复的行并将其显示在表中包含该值的所有数据(目前有3列,“matricula”,“marca”和“despesa”)

下面是我用来从下拉列表中获取值的代码,这就是我遇到问题的地方,我无法从下拉列表中获取值来显示表中的相应行。

 <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();
?>

我在这个问题上已经有一段时间了,在做这个问题之前我做了很多研究。

首先..我认为你的语法是错误的。

试试这个:

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

在查询中,您必须将php变量放在引号之间。

matricula是一个字段来自表,在你的帖子...不是一个PHP变量..所以你必须有:GROUP BY matricula HAVIG count(matricula)

请确保您的SQL语法正确并尝试使用此代码从返回的数据库值创建表以使其成为单个表

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>";

如果您在表中检查发布值的完全匹配,为什么不能使用它?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM