【问题标题】:I am currently trying to populate dropdown menus with a mysql server from phpMyAdmin我目前正在尝试使用 phpMyAdmin 的 mysql 服务器填充下拉菜单
【发布时间】:2013-04-18 21:38:33
【问题描述】:

index.html:

<?php
    include_once 'func.inc.php';
    connect();
?>

<!doctype html>
<html>
<head>
    <title>Team DriveSmart</title>
</head>
<body bgcolor="#008933" text="#ffffff">
    <header>
        <h1 align="center"> Team DriveSmart </h1>
    </header>
    <header>
        <h3 align="center"> Highway State </h3>
    </header>
    <section>
        <article>
            <hgroup>
            </hgroup>
            <p align="center">
            <form style="text-align:center" method="post" action="">

                <select name="dropdown1">
                    <?php query1() ?>   
                </select>

            <input type="submit" value="Submit" />
            </form> 
            </p>
        </article>
        <article>
        </article>
    </section>
    <footer>
        <f1 align="center"></f1>
    </footer>



    <header>
        <h3 align="center"> Highway ID </h3>
    </header>
    <section>
        <article>
            <hgroup>
            </hgroup>
            <p align="center">
            <form style="text-align:center" method="post" action="">

                <select name="dropdown2">
                    <?php query2() ?>   
                </select>

            <input type="submit" value="Submit" />
            </form> 
            </p>
        </article>
        <article>
        </article>
    </section>
    <footer>
        <f1 align="center"></f1>
    </footer>

</body>
</html>

然后我的 func.inc.php 看起来像这样:

<?php
    include_once 'db.inc.php';

    function connect()
    {
        mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Error connecting" . mysql_error()) ;
        mysql_select_db(DB_NAME);
    }

    function close()
    {
        mysql_close();
    }

    function query1()
    {

        $myData=mysql_query("SELECT * FROM highways_highway");
        while($record = mysql_fetch_array($myData))
        {
            echo '<option value="' . $record['highway_state'] . '">' . $record['highway_state'] . '</option>' ;
        }
    }

    function query2()
    {
        $myData=mysql_query("SELECT * FROM highways_exit");
        while($record = mysql_fetch_array($myData))
        {
            echo '<option value="' . $record['highway_id'] . '">' . $record['highway_id'] . '</option>' ;
        }
    }


?>

我想做的是用数据库中的选项填充这些下拉菜单(例如,在 query1 中,我想用状态填充菜单)。代码目前似乎显示了一个下拉菜单和一个提交按钮,但下拉菜单是空的,我不知道为什么。

B 部分:一旦用户从下拉菜单中进行选择,我想根据查询 SELECT * FROM Highways_highway WHERE Highway_state = dropdown1 显示整个 sql 表,一旦 A 部分修复,有什么建议吗?

【问题讨论】:

  • @chandresh_cool 他没有返回任何数据
  • 我是php新手,返回数据怎么办?
  • 不,Chandresh 说您需要像 echo function_name() 这样回显您的函数,但您已经在函数中回显,但您应该返回值而不是回显
  • 您的表中是否填充了数据?有什么错误吗? (此外,您真的应该避免使用已弃用的 mysql_* 函数)
  • mysql 折旧了?我应该尝试mysqli吗?

标签: php mysql


【解决方案1】:

试试这个

function my_custom_function()
{
// php code here

return $myData;
}

或者在你的情况下

function query2()
    {
     $myHtmlData = '';

        $myData=mysql_query("SELECT * FROM highways_exit");
        while($record = mysql_fetch_array($myData))
        {
            $myHtmlData .= '<option value="' . $record['highway_id'] . '">' . $record['highway_id'] . '</option>' ;
        }

   return $myHtmlData;
    }

【讨论】:

  • 这似乎不起作用,菜单仍然显示为空白。我还应该在 index.html 中做些什么来接收返回值吗?
【解决方案2】:

使用这个。

在 index.html 文件中,像这样回显你的函数。

<select name="dropdown1">
     <?php echo query1(); ?>   
</select>

<select name="dropdown2">
     <?php echo query2(); ?>   
</select>

在 func.inc.php 文件中,像这样返回数据。

function query1()
{
    $myselectData = '';

    $myData= mysql_query("SELECT * FROM highways_highway");
    while($record = mysql_fetch_assoc($myData))
    {
        $myselectData .= '<option value="'.$record['highway_state'].'">' . $record['highway_state'] . '</option>';
    }

  return $myselectData; 
}

function query2()
{
    $myselectData1 = '';

    $myData= mysql_query("SELECT * FROM highways_exit");
    while($record = mysql_fetch_assoc($myData))
    {
        $myselectData1 .= '<option value="'.$record['highway_id'].'">' . $record['highway_id'] . '</option>';
    }

 return $myselectData1; 
}

这可能对你有帮助。

【讨论】:

  • 感谢您的回复。似乎我仍然得到相同的结果。 [链接]drivesmart.web.engr.illinois.edu
  • @KyleGrage 检查我的编辑答案并尝试一下。如果有任何错误,请告诉我。
【解决方案3】:

显然,问题是我将文件命名为 index.html 而不是 index.php,对不起,感谢您的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 2019-11-26
    • 2015-07-04
    • 1970-01-01
    • 2020-03-31
    相关资源
    最近更新 更多