2012-03-03 58 views
0

我不知道是否有人可以幫助我。PHP下拉菜單檢索結果

我把下面的腳本放在一起,顯示了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> 
+0

在您的選擇查詢中沒有findid。 – Rufinus 2012-03-03 15:37:15

+0

我不確定我是否瞭解您的問題。你有這個下拉菜單,根據用戶的選擇,你需要從你的bd中檢索一些數據? – mddw 2012-03-03 15:39:21

+0

兩者,道歉,我已經修改我的帖子更準確。親切的問候 – IRHM 2012-03-03 15:46:42

回答

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>"; 
+0

嗨,非常感謝。我已經整理好了這個查詢並修改了我原來的帖子以反映這一點。不過,我仍然保持原樣,因爲您的建議會將值放在下拉菜單之外。親切的問候 – IRHM 2012-03-03 16:09:28

+0

好的:)你的歡迎。另外1件事...下拉菜單可以在用戶端和服務器端的php上運行。 – qpaycm 2012-03-03 16:14:32

1

你的表單需要一個目的地PHP文件提交,和一個提交按鈕:

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

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

handle_sumbission.php將收到的形式選擇作爲$ _POST中的條目。我會從這樣一個單獨的腳本開始,一旦你得到它的工作,你可以將它們摺疊成一個腳本,如果你真的想。

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"; 
0

作爲媒體鏈接提到的下拉菜單將使用側。如果你只想使用php,你可以發送GET值到頁面。捕捉該值並使用它進行查詢...

在下拉列表中的鏈接可能是:

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

你會抓住它想:

$date = $_GET['date']; 

然後你不得不改變你的查詢並添加$日期爲WHERE子句,如:

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

不知道,如果我給你所需要的......當然,你應該檢查這個GET值和再在查詢中使用它之前,先查看它...