PHP下拉菜单以检索结果

时间:2012-03-03 15:33:55

标签: php mysql

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

我将下面的脚本放在一起,显示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>

4 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分:1)

这是一个应该完成这项工作的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"; 

答案 2 :(得分: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>";

答案 3 :(得分:0)

如上所述,下拉菜单将是用户名。如果您希望仅使用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 ...