【问题标题】:Creating a dropdown in my webpage with selected entries from my database在我的网页中创建一个下拉列表,其中包含从我的数据库中选择的条目
【发布时间】:2015-10-13 13:07:05
【问题描述】:

我正在创建一个网页,用户可以在该网页中输入名称,并在下拉列表中显示以该名称注册的市场。 这是我的部分代码

            <form name="login" action="index_submit" method="get" accept-charset="utf-8">
        <ul>
            <li>
                <label for="seller">Seller Name/ID</label>
                <input type="text" id="username" placeholder="Seller Username/ID" required>
            </li>
             <?php

             ?>
            <li>
                <label for="marketplace">Choose a marketplace</label>
                <select name="url" onchange="menu_goto(this.form)">
                <?php
                    include ('config.php');
                    $username = $_POST['username'];
                    $query = "SELECT Marketplace FROM Details WHERE Seller_name='$username' OR Seller_ID='$username'";
                    $result = mysql_query($dbcon, $query) or die('error getting data');
                    while ($row = mysql_fetch_array($result)){
                    echo "<option value="marketplace1">" . $row['Marketplace'] . "</option>";
                    }
                ?>
                </select>
            </li>

但是当我运行 .php 文件时,我得到一个屏幕,直到遇到 php 脚本,它下面的 html 代码保持不变。我是新手。我无法在我的代码中检测到错误(如果有)。 config.php 文件只是与我的数据库建立的连接(其中不包含更多代码)。此外,如果我从代码中删除 php 脚本然后显示我的网页,我的其余代码工作正常。我猜问题出在我的 php 脚本上。

编辑:我使用了某人回答的以下内容: 在用户名输入字段上,放置一个 javascript 函数,然后使用 ajax 调用服务器端代码,然后将结果作为选项附加到 url 字段。喜欢以下。

    <form name="login" action="index_submit" method="get" accept-charset="utf-8">
    <ul>
        <li>
            <label for="seller">Seller Name/ID</label>
            <input type="text" onchange="ajaxCall(this.value)" id="username" placeholder="Seller Username/ID" required>
        </li>
         <?php

         ?>
        <li>
            <label for="marketplace">Choose a marketplace</label>
            <select name="url" onchange="menu_goto(this.form)">
            </select>
        </li>

javascript函数应该是这样的。

          <script>
           function ajaxCall(val)
            {
          $.post('abc.php',{'username':val},function(res){
           $.('#url').html(res);
           });
           }
        </script>

abc.php 将跟随。

                <?php
                include ('config.php');
                $username = $_POST['username'];
                $query = "SELECT Marketplace FROM Details WHERE Seller_name='$username' OR Seller_ID='$username'";
                $result = mysql_query($dbcon, $query) or die('error getting data');
                $res='';
                while ($row = mysql_fetch_array($result)){
                $res .='<option value="marketplace1">' . $row['Marketplace'] . '</option>';
                }
             echo $res;
             ?>

我的整个代码现在正在执行,但我的下拉列表中仍然没有任何值。(它显示为空白,是的,我的数据库中有值)。

【问题讨论】:

  • 好吧,首先你把mysql_query()的参数弄错了,做$result = mysql_query($query, $dbcon)或者你可能正在使用MYSQLI_扩展,它只是一个TYPO,它应该是mysqli_query($dbcon, $query)跨度>
  • Deprecated mysql* Please, don't use mysql_* functions in new code. 它们不再维护,已正式弃用。使用 mysqli 或 PDO
  • 搜索报错信息
  • 看看这个how to use mysqli and Proper error processing t'internet 上还有更多内容
  • @RiggsFolly,我正在使用 mysql 并实习 $dbcon 和 $query 没有帮助。结果还是一样。

标签: php mysql database drop-down-menu command-line


【解决方案1】:
echo "<option value="marketplace1">" . $row['Marketplace'] . "</option>";
                    ^            ^ ---------> Not escaped

试试

echo "<option value=\"marketplace1\">" . $row['Marketplace'] . "</option>";

或者

echo '<option value="marketplace1">' . $row['Marketplace'] . '</option>';

希望对你有帮助。

PS:看看 MySQLi 和/或 PDO,因为不推荐使用 mysql 函数。

编辑:如果没有错误,你有 error_reporting 吗?如果不插入

error_reporting(E_ALL);

进入你的文档,你应该得到这样的东西:

Parse error: syntax error, unexpected 'marketplace1' (T_STRING), expecting ',' or ';' in C:\xampp\htdocs\testing\index.php on line 2

【讨论】:

  • 我不确定 error_reporting(E_ALL) 的作用,尽管在我的 php 脚本中添加它并没有什么不同。我是否需要为我的 php 代码添加提交以读取查看者输入的用户名的市场?
  • 查看您的php error log,如果您在服务器设置中对此进行测试,因为实时错误不会显示在浏览器上,但可能会被记录到错误日志中
【解决方案2】:

假设您已经完成了@yaso 建议的 HTML 更改

像这样改变你的代码,它应该设置错误显示在浏览器窗口上。

我还假设您使用

之类的 url 启动它

example.com/myscript.php?username=OneThatExists

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    include ('config.php');

    $username = $_POST['username'];
    $query = "SELECT Marketplace FROM Details WHERE Seller_name='$username' OR Seller_ID='$username'";
    $result = mysql_query($query,$dbcon) or die('error getting data');

    while ($row = mysql_fetch_array($result)){
        echo "<option value="marketplace1">" . $row['Marketplace'] . "</option>";
    }
?>

你真的应该测试一个参数实际上是这样传递的。你可以写sanityCheck()

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    include ('config.php');

    if ( isset($_POST['username']) ) {

        $_POST['username'] = sanityCheck($_POST['username']);

        $query = "SELECT Marketplace 
                  FROM Details 
                  WHERE Seller_name='{$_POST['username']' 
                     OR Seller_ID='{$_POST['username']}'";

        $result = mysql_query($query, $dbcon) or die('error getting data');

        while ($row = mysql_fetch_array($result)){
            echo "<option value="marketplace1">" . $row['Marketplace'] . "</option>";
        }
    } else {
        // do whatever you want to do if no parameter was entered
    }
?>

【讨论】:

    猜你喜欢
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-24
    • 2021-02-08
    • 1970-01-01
    相关资源
    最近更新 更多