【问题标题】:use drop down menu to query mysql使用下拉菜单查询mysql
【发布时间】:2013-05-23 03:32:23
【问题描述】:

这里是初学者;我已经搜索了很多例子,仍然需要一些帮助。 我希望用户从下拉框中选择一个选项,该选项将查询 mysql 中的表。我不明白如何(在我的 html 文件中)在 getprojectstatus.php 中显示 select 语句。

<html>
<head>
    <title>Status Dashboard</title>
</head>

<body style="background:#19245e url('images/fade.png')repeat-x;font-size:12px;font-family: Arial;line-height:18px;color:#FFFFFF;">
    <script>
        function displayProject(option)
            {
            var x;
            if (option=='sciplay')
            {                        
             x="show Sciplay selected, show notes, status, etc..."
            }

            else if (option=='oklahoma')
            {
             x="show OK selected, show..."
            }

            else if (option=='northdakota')
            {
             x="show North Dakota selected, show..."
            }

            else if (option=='audit')
            {
             x="show Audit selected, show..."
            }

            else if (option=='sggaming')
            {                           
             x="show SG Gaming selected, show..."
            }

            else if (option=='all')
            {
             x="..."//(option=(1+2+3+4+5))
            }
            document.getElementById("demo").innerHTML=x;
            }

    </script>

    <div align="center">
        <TABLE BORDER="1">
        <TR>
            <TD><img src='images/header.png'/>
            </TD>
        </TR>

        <TR>
            <TD>
                <TABLE BORDER="0" bgcolor="C0C0C0" align="left">
                    <TH>Projects
                    </TH>
                    <TR>
                        <TD>
                            <FORM action="getprojectstatus.php" method="post">
                                <SELECT onload="displayProject(this.value);" onchange="displayProject(this.value);">
                                    <OPTION VALUE='all'>ALL</OPTION>
                                    <OPTION VALUE='sciplay'>Sciplay</OPTION>
                                    <OPTION VALUE='oklahoma'>Oklahoma</OPTION>
                                    <OPTION VALUE='northdakota'>North Dakota</OPTION>
                                    <OPTION VALUE='audit'>Audit SSAE16</OPTION>
                                    <OPTION VALUE='sggaming'>SG Gaming</OPTION>
                                </SELECT>
                            </FORM>
                        </TD>
                    </TR>

                </TABLE>
            </TD>
        </TR>

        <TR>
            <TD>
                <p id="demo">
                </p>
            </TD>
        </TR>

        </TABLE>

    </div>
</body>

还有我的 getprojectstatus.php 文件:

<?php
//create connection variables
$host=`localhost`;
$user=`dashboard`;
$pw=`password`;
$db=`status`;
//create connection
$con=mysqli_connect("$host", "$user", "$pw", "$db");
//check connection
if (mysqli_connect_errno($con))
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

//take input from form and store as var queries
$option= $_POST['option'];

if ($option == 'sciplay')
{
 $queries = "SELECT * FROM status where project=1"
}

else if ($option == 'oklahoma')
{
 $queries = "SELECT * FROM status where project=2"
}

else if ($option == 'northdakota')
{
 $queries = "SELECT * FROM status where project=3"
}

else if ($option == 'audit')
{
 $queries = "SELECT * FROM status where project=4"
}

else if ($option == 'sggaming')
{
 $queries = "SELECT * FROM status where project=5"
}

else ($option == 'all')
{
 $queries = "SELECT * FROM status"
}

//store query as var result
$queries=$query;
$result=@mysqli_query($con,"$query");
//echo var result in table format
echo "<table border='1'>
<tr>
<th>Project</th>
<th>Subject</th>
<th>Status</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['project'] . "</td>";
echo "<td>" . $row['subject'] . "</td>";
echo "<td>" . $row['entry'] . "</td>";
echo "</tr>";
}
echo "</table>";
//close mysql connection
mysqli_close($con);
?>

提前感谢您的帮助!

【问题讨论】:

  • 你能解释一下你卡在哪里了吗?究竟是什么不适合你?
  • 当我从下拉列表中选择一个选项时,php中的select语句不显示。
  • 您的 html 文件的名称是什么?您是否在此处显示了整个文件,或者那里是否有其他 PHP 文件?我请求帮助我了解对您的页面如何运作的理解。您在数据的传递方式上犯了一个简单的(并且在开始时很常见)错误计算。

标签: php html mysql


【解决方案1】:

好的,看来您还没有完全掌握基于 Web 的系统中使用的客户端/服务器模型(我们一开始都在那里)。

这里有一个简要说明,然后我们将讨论您的问题所在。

  1. 您打开浏览器并输入 URL,然后按 Enter 键
  2. 您的浏览器向托管您输入的 URL 的服务器发送请求
  3. 服务器解析请求并加载所请求的特定文件。就我们而言,这意味着运行 PHP 文件以最终生成 HTML 和 javascript 输出
  4. 它将这个 HTML(和 javascript)发送到浏览器。从来没有将可执行的 PHP 发送到浏览器,它都保留在服务器上。
  5. 您的浏览器接收此 HTML(和 javascript)并显示它。
  6. 您的浏览器可以执行页面上的任何 javascript,但如果它需要页面源中不存在的信息,它必须从服务器请求它。它可以通过以下两种方式之一执行此操作:a)您可以使用正确的参数向服务器发送一个全新的请求,以便预先包含您想要的信息,或者 b)您可以运行 ajax 请求来提取您的信息需要,然后将其插入到您的页面中

因此,乍一看,故障似乎发生在第 5 步和第 6 步之间,您已经在浏览器中加载了整个页面,它需要来自服务器的更多信息,但您' 从来没有真正将该请求发送到服务器。如果您将该请求发送到服务器(通过向您的表单添加一个提交按钮,或者向您的displayProject() 函数添加一个表单提交请求),那么您没有办法(目前)将该新信息插入到您现有的页面中。

因此,您的问题有两个可能的答案:

如果您想更好地了解如何编写 PHP 应用程序,因为您打算在未来做更多这样的事情:

...那么您应该将这两个单独的文件重写为一个文件。 (我已经按原样删除了您的代码,我还没有检查任何错误)

<?php
//create connection variables
$host=`localhost`;
$user=`dashboard`;
$pw=`password`;
$db=`status`;
//create connection
$con=mysqli_connect("$host", "$user", "$pw", "$db");
//check connection
if (mysqli_connect_errno($con))
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

//take input from form and store as var queries
$option= $_POST['option'];

if ($option == 'sciplay')
{
    $queries = "SELECT * FROM status where project=1"
}

else if ($option == 'oklahoma')
{
    $queries = "SELECT * FROM status where project=2"
}

else if ($option == 'northdakota')
{
    $queries = "SELECT * FROM status where project=3"
}

else if ($option == 'audit')
{
    $queries = "SELECT * FROM status where project=4"
}

else if ($option == 'sggaming')
{
    $queries = "SELECT * FROM status where project=5"
}

else ($option == 'all')
{
    $queries = "SELECT * FROM status"
}

//store query as var result
$queries=$query;
$result=@mysqli_query($con,"$query");

//The actual echo of the table display was moved to the area of the page it actually needs to go

//close mysql connection
mysqli_close($con);
?>
<html>
<head>
    <title>Status Dashboard</title>
</head>

<body style="background:#19245e url('images/fade.png')repeat-x;font-size:12px;font-family: Arial;line-height:18px;color:#FFFFFF;">
    <script>
    function displayProject(option)
        {
        var x;
        if (option=='sciplay')
        {                        
         x="show Sciplay selected, show notes, status, etc..."
        }

        else if (option=='oklahoma')
        {
         x="show OK selected, show..."
        }

        else if (option=='northdakota')
        {
         x="show North Dakota selected, show..."
        }

        else if (option=='audit')
        {
         x="show Audit selected, show..."
        }

        else if (option=='sggaming')
        {                           
         x="show SG Gaming selected, show..."
        }

        else if (option=='all')
        {
         x="..."//(option=(1+2+3+4+5))
        }
        document.getElementById("demo").innerHTML=x;
        }

    </script>

    <div align="center">
        <TABLE BORDER="1">
        <TR>
            <TD><img src='images/header.png'/>
            </TD>
        </TR>

        <TR>
            <TD>
                <TABLE BORDER="0" bgcolor="C0C0C0" align="left">
                    <TH>Projects
                    </TH>
                    <TR>
                        <TD>
                            <!--
                            SOME CHANGES HERE: The form action is now pointing
                            to this same file, so that when you submit your form,
                            it goes back to this same file.
                            Also, I gave your select tag a "name" attribute so
                            that when you submit it, it'll actually be accessible
                            in the $_POST['option'] variable 
                            -->
                            <FORM action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                                <SELECT name="option" onload="displayProject(this.value);" onchange="displayProject(this.value);">
                                    <OPTION VALUE='all'>ALL</OPTION>
                                    <OPTION VALUE='sciplay'>Sciplay</OPTION>
                                    <OPTION VALUE='oklahoma'>Oklahoma</OPTION>
                                    <OPTION VALUE='northdakota'>North Dakota</OPTION>
                                    <OPTION VALUE='audit'>Audit SSAE16</OPTION>
                                    <OPTION VALUE='sggaming'>SG Gaming</OPTION>
                                </SELECT>
                                <input type="submit" value="Reload Form">
                            </FORM>
                        </TD>
                    </TR>

                </TABLE>
            </TD>
        </TR>

        <TR>
            <TD>
                <!-- a <p> tag can't technically hold a <table> tag, but one thing at a time here -->
                <p id="demo">
                    <?php
                    // I'm just lifting your structure out and placing it here for clarity purposes.
                    // really, you can just write these tags out in HTML directly,
                    // not go into PHP and echo them
                    echo "<table border='1'>
                        <tr>
                            <th>Project</th>
                            <th>Subject</th>
                            <th>Status</th>
                        </tr>";

                    while($row = mysqli_fetch_array($result))
                    {
                        echo "<tr>";
                        echo "<td>" . $row['project'] . "</td>";
                        echo "<td>" . $row['subject'] . "</td>";
                        echo "<td>" . $row['entry'] . "</td>";
                        echo "</tr>";
                    }
                    echo "</table>";
                    ?>
                </p>
            </TD>
        </TR>

        </TABLE>

    </div>
</body>

...所以,这里发生的事情是,我已经对其进行了更改,以便在同一文件中构建和使用数据。您的页面现在需要请求新信息,一切正常,如果您想更改表单,那么您提交并再次请求整个页面,现在包含新信息。

这一切都很好,但可能不是您希望页面的工作方式。也就是说,您应该了解该过程是如何运作的,因为即使是基本复杂的任何 PHP 应用程序都需要至少部分地以这种方式工作。

现在... 如果您只是希望此页面按照您希望的方式运行,即您构建页面,然后请求新信息以根据表单请求更新您的页面:强>

...那我们就可以完成了。同样,我没有对错误进行任何检查。不过,首先,把它放在你的 &lt;head&gt; 标签中:

<head>
    <title>Status Dashboard</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
</head>

...我们将使用 jquery,只是因为在我们这里的例子中,它让我们只需要担心运行 ajax 请求而不是设置所有必要的输入和输出。然后你想把它添加到你的 displayProject 函数中(就在关闭 } 之前):

$.ajax({
    url: 'getprojectstatus.php',
    type: 'post',
    data: 'option='+option,
    success: function(data) {
        $('#demo').html(data);
    }
});

...所有发生的事情就是您将“选项”选择发送到getprojectstatus.php,该文件正在为您的表生成HTML输出,然后一旦收到,它就会运行“成功”,它只是将数据直接插入到具有“演示”id 属性的元素中。您至少应该考虑将其更改为 &lt;div&gt;,因为 &lt;p&gt; 可能会引起头痛。

【讨论】:

  • 非常全面的答案。如果 StackOverflow 让我投票两次,我会的。
【解决方案2】:

我不确定我是否正确理解了这个问题,但这可能对你有用:

<FORM action="getprojectstatus.php" method="post">
 <SELECT onload="displayProject(this.value);" onchange="displayProject(this.value);">
  <OPTION VALUE='none'>ALL</OPTION>
  <OPTION VALUE='1'>Sciplay</OPTION>
  <OPTION VALUE='2'>Oklahoma</OPTION>
  <OPTION VALUE='3'>North Dakota</OPTION>
  <OPTION VALUE='4'>Audit SSAE16</OPTION>
  <OPTION VALUE='5'>SG Gaming</OPTION>
 </SELECT>
</FORM>

然后在你的 PHP 文件中:

$option= $_POST['option'];

$queries = "SELECT * FROM status"
if ($option != 'none'){
 $queries = "SELECT * FROM status where project=".$option
}

我还建议研究一些 PHP 框架,可能是 CodeIgniter 并学习 MVC 模式。

【讨论】:

  • 感谢 Teodor,这是更优雅的 PHP 代码。我认为我没有清楚地提出我的问题。当用户单击下拉菜单中的选项时,我希望 mysql 表中的结果在 HTML 页面中显示给用户。我希望 html 中的 var x 从 php 查询中回显 select 语句
  • 您的代码的确切问题是什么。任何错误或不做你想做的事。请帮我理解。
  • 我的代码没有做我想做的事。如何在我的函数 displayProject 中设置 var x 以显示在 php 中运行的查询?
猜你喜欢
  • 2018-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-14
  • 1970-01-01
  • 2012-11-19
  • 2019-03-05
相关资源
最近更新 更多