gpt4 book ai didi

php - 列出 MySQL DB 中的表并将表名称显示为下拉列表中的选项

转载 作者:行者123 更新时间:2023-11-30 00:48:54 25 4
gpt4 key购买 nike

我正在尝试编写一个包含 2 个下拉列表的页面。第一个从 mysql 数据库填充表列表,允许用户选择相关日期,第二个允许用户从该表的列中选择名称。我目前的表单如下所示,用于从用户指定的特定表/行生成值数组,然后我可以将其回显到 html 表单中的各个字段。

我正在努力让下拉列表填充表名称。有人可以帮忙吗?抱歉,这是我的第一篇文章,我想写得很长。我知道当我在代码中指定要使用的表时,与数据库的连接正在工作,并且第二个下拉列表正在工作。

我需要第一个框来列出表名称,然后设置变量 $date。

这是日期的下拉列表:

<select name='Date'>
<?php
$date = $_POST['date'];
$cxn=mysqli_connect("localhost",$userid,$password,$db)
or die ("Could not connect to Database");

$result=mysqli_query($cxn,"Show tables from january");

while($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{
echo "<option value='".$row['Value']."'>".$row['Value']."</option>";
}

?>
</select>

这是上面所选表格中名称的下拉列表。

<select name='agentname'>
<?php
$name = $_POST['agentname'];
$cxn=mysqli_connect("localhost",$userid,$password,$db)
or die ("Could not connect to Database");

$sql=mysqli_query($cxn,"SELECT Value FROM january.'$date'");

while($row = mysqli_fetch_assoc($sql))
{
echo $value ="<option value='".$row['Value']."'>".$row['Value']."</option>";
}

?>
</select>
<input type='submit' value='Get Dashboard' />
</form>

创建表单:

<form action='getlist.php' method='post'>
<div align="center">
<p>
<select name='Date'>
<?php
$date = $_POST['date'];
$cxn=mysqli_connect("localhost",$userid,$password,$db)
or die ("Could not connect to Database");

$result=mysqli_query($cxn,"Show tables from january");

while($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{
echo "<option value='".$row['Value']."'>".$row['Value']."</option>";
}

?>
</select>

<select name='agentname'>
<?php
$name = $_POST['agentname'];
$cxn=mysqli_connect("localhost",$userid,$password,$db)
or die ("Could not connect to Database");

$sql=mysqli_query($cxn,"SELECT Value FROM january.'$date'"); //I have specified schema's in my DB for the different months

while($row = mysqli_fetch_assoc($sql))
{
echo $value ="<option value='".$row['Value']."'>".$row['Value']."</option>";
}

?>
</select>
<input type='submit' value='Get Dashboard' />
</form>

要在以下查询中使用来生成数组:

<?php
$cxn=mysqli_connect("localhost",$userid,$password,$db)
or die ("Could not connect to Database");

$result=mysqli_query($cxn,"SELECT column1, column2, column3 //just listing examples here for my columns there is about 45 columns for this selection
FROM january.'$date'
WHERE Value='$name'");

while($row = mysqli_fetch_array($result))
{
$stats = $row;
}

?>

最佳答案

好吧...

为了解决您的实际问题,我认为问题在于您在 mysqli_fetch_assoc 的输出中引用的索引。通常,“SHOW”查询的表名称的键控类似于“Tables_in_databasename”或类似的内容。您可以print_r您的$row变量来查看您应该引用哪个索引。

现在,与您的问题无关,我确实必须坚持要求您重新考虑如何存储数据。在数据库和表的名称中表示有意义的数据是非常糟糕的过程。这不仅不是数据库应有的使用方式,这将不可避免地给您带来问题,而且还会给您带来不必要的困难。

我不确定您要存储哪种统计数据,但假设它是网站每天的点击次数。这些数据可以非常轻松地存储在一个数据库和一张表中。例如:

数据库:统计

表:daily_statistics

列:id、stat_date、值

现在您可以轻松地将每天的点击次数存储在一张表中。

id, stat_date, value
1, 2014-01-01, 300
2, 2014-01-02, 400
etc...

假设您每天有多个值。没问题

id, stat_date, value
1, 2014-01-01, 300
2, 2014-01-02, 400
3, 2014-01-02, 250
etc...
// The unique ID, alows multiple records per day.
// If you need them categorized in a different way,
// add another column.

这种结构将在未来的岁月里发挥作用。

id, stat_date, value
1, 2014-01-01, 300
2, 2014-01-02, 400
3, 2014-01-02, 250
4, 2015-01-01, 250
5, 2016-01-01, 250
etc...

现在您可以访问 mysql 聚合函数,您将更容易导入和导出数据,随着时间的推移,您将不必处理数百个数据库甚至更多的表,并且任何追随您的开发人员都不必费尽心思来解决这种棘手的情况。

关于php - 列出 MySQL DB 中的表并将表名称显示为下拉列表中的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21147012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com