【问题标题】:PHP Drop Down Menu To Retrieve ResultsPHP 下拉菜单检索结果
【发布时间】:2012-03-03 15:33:54
【问题描述】:

我想知道是否有人可以帮助我。

我将下面的脚本放在一起,显示来自 mySQL 数据库的日期列表。

<?php
mysql_connect("host", "user", "password")or
die(mysql_error());
mysql_select_db("database");
?>
<form>
<select>

<?php 
$result = "SELECT userdetails.userid, finds.dateoftrip, detectinglocations.locationname, finds.findid, finds.userid, finds.locationid, detectinglocations.locationid, finds.findname, finds.finddescription FROM userdetails, finds, detectinglocations WHERE finds.userid=userdetails.userid AND finds.locationid=detectinglocations.locationid AND finds.userid = 1 GROUP By dateoftrip ORDER BY dateoftrip DESC";     
$result =mysql_query($result);
while ($data=mysql_fetch_assoc($result)){
?>
<option value ="<?php echo $data['findid'] ?>" ><?php echo $data['dateoftrip'] ?></option>
<?php } ?>

</select>
</form>

我现在想做的是在选定日期后,从“findname”和“finddescription”字段中检索相关值,并将它们插入到我页面上的表格中。

我花了几个小时试图让它工作,但没有任何成功。我只是想知道是否有人可以帮我一把,让我知道我需要做什么来检索结果。

非常感谢

更新代码

<?php
mysql_connect("host", "user", "password")or
die(mysql_error());
mysql_select_db("database");
?>
<form>
<select>

<?php 
$result = "SELECT dateoftrip, findid, userid, locationid, findname, finddescription FROM finds GROUP By dateoftrip ORDER BY dateoftrip DESC";  

$result =mysql_query($result);
while ($data=mysql_fetch_assoc($result)){
?>
<option value ="<?php echo $data['findid'] ?>" ><?php echo $data['dateoftrip'] ?></option>
<?php } ?>

</select>
</form>

【问题讨论】:

  • 您的选择查询中没有 findid。
  • 我不确定我是否理解您的问题。你有这个下拉菜单,根据用户的选择,你需要从你的 bd 中检索一些数据?
  • 两者,抱歉,我已经修改了我的帖子以更准确。亲切的问候

标签: php mysql


【解决方案1】:

让我知道我需要做什么来检索结果

你需要的是:

1.修复查询

SELECT 
   userdetails.userid AS userdetails_userid, 
   finds.dateoftrip, 
   detectinglocations.locationname, 
   finds.userid AS finds_userid, 
   finds.locationid AS finds_locationid , 
   detectinglocations.locationid AS detectinglocations_locationid ,
   finds.findname, 
   finds.finddescription 
FROM 
   userdetails, 
   finds, 
   detectinglocations 
WHERE 
   finds.userid=userdetails.userid AND 
   finds.locationid=detectinglocations.locationid AND 
   finds.userid = 1 
GROUP By 
   finds.dateoftrip
ORDER BY 
   finds.dateoftrip DESC

2.将生成代码插入变量,然后回显

while ($data=mysql_fetch_assoc($result)) {
   $options .="<option value =\"". $data['userdetails_userid'] ."\">". $data['finds_locationid'] ."</option>";
}
echo "<select>". $options ."</select>";

【讨论】:

  • 嗨,非常感谢。我已经大大整理了查询并修改了我的原始帖子以反映这一点。但是,我确实保留了表单,因为您的建议将值放在下拉菜单之外。亲切的问候
  • 好的 :) 欢迎您。还有 1 件事...下拉菜单在用户端有效,php 在服务器上有效。
【解决方案2】:

您的表单需要一个要提交的目标 php 文件和一个提交按钮:

<form method="post" action="handle_submission.php">
...

<input type="submit" value="Search" name="search"/>
</form>

handle_sumbission.php 将接收表单选择作为 $_POST 中的条目。我会从这样一个单独的脚本开始,一旦你让它工作,如果你真的想的话,你可以把它们合并成一个脚本。

【讨论】:

    【解决方案3】:

    这是一个 AJAX 脚本,应该可以完成这项工作。

    //必须包含JQuery库

    //页面脚本

    <script type="text/javascript">
           $(document).ready(function() {
            $("#findname").blur(function()
                {
                 //remove all the class add the messagebox classes and start fading
                 $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
                 //check the username exists or not from ajax
                 $.post("user_availability.php",{ username:$(this).val() } ,function(data)
                 {
                  if(data=='no') //if username not avaiable
                  {
                   $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
                   {
                    //add message and change the class of the box and start fading
                    $(this).html('Username not available to register').addClass('messageboxerror').fadeTo(900,1);
                   });
                  }
                  else
                  {
                   $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
                   {
                    //add message and change the class of the box and start fading
                    $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);
                   });
                  }
                 });
                });
           });
        </script>
    

    将您的 PHP mySQL 查询放入名为 user_availability.php 的脚本中,并在用户离开该字段后执行该脚本。如果 mySQL 找到匹配项,该脚本正在寻找“是”或“否”,但您也可以让 PHP 脚本返回您想要的任何内容。

    以下是 PHP 脚本如何工作的示例:

    // Connect to MYSQL database //
        $host = 'localhost';
        $user = 'root';
        $pass = 'root';
        $db = 'table';  
    
    
    
        $connect = mysql_connect($host,$user,$pass) or die ("Couldn't connect to mySQL!");
        mysql_set_charset('utf8',$connect);
        mysql_select_db($db) or die ("Couldn't find the database");
    
    // Form value sent by AJAX
        $user_name=$_POST['username'];
        $length = strlen($user_name);
        if ($length < 5) {
          echo "no"; exit();
        }
    
    // Grab all users in the database
    $query = "SELECT username FROM users";
    $result = mysql_query($query);
    $count = mysql_num_rows($result); $i=0;
    
    while ($i < $count) {
      $existing_user = mysql_result($result, $i, 'username');
         if ($user_name == $existing_user) 
         {
            exit();
         }
      $i++;   
    }
    //No matches so return OK to proceed with name 
    echo "yes"; 
    

    【讨论】:

      【解决方案4】:

      如前所述,下拉菜单将在用户端。如果您希望它仅与 php 一起使用,您可以将 GET 值发送回页面。捕获该值并使用它进行查询...

      下拉列表中的链接可能是:

      <a href="mypage.php?date=2012-03-08">datetodisplay</a>
      

      你会像这样抓住它:

      $date = $_GET['date'];
      

      然后您必须更改您的查询并将 $date 添加到 WHERE 子句中,例如:

      $query = "SELECT * FROM WHERE date = " . $date;
      

      不知道我是否给了你需要的东西......当然你应该在查询中使用它之前检查这个 GET 值并 real_escape 它......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-12
        • 1970-01-01
        • 2021-12-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多