用于检索结果的PHP下拉菜单
我不知道是否有人能帮我 下面的脚本显示了mySQL数据库中的日期列表用于检索结果的PHP下拉菜单,php,mysql,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, detectingloc
<?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>
您的表单需要一个要提交到的目标php文件和一个提交按钮:
<form method="post" action="handle_submission.php">
...
<input type="submit" value="Search" name="search"/>
</form>
...
handle\u sumbision.php
将以$\u POST中的条目形式接收表单选择。我会从这样一个单独的脚本开始,一旦你开始工作,如果你真的想,你可以将它们折叠成一个脚本。你的表单需要一个目标php文件提交到,以及一个提交按钮:
<form method="post" action="handle_submission.php">
...
<input type="submit" value="Search" name="search"/>
</form>
...
handle\u sumbision.php
将以$\u POST中的条目形式接收表单选择。我会从这样一个单独的脚本开始,一旦你让它工作起来,如果你真的想,你可以将它们折叠成一个脚本。这里有一个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>
$(文档).ready(函数(){
$(“#findname”).blur(函数()
{
//删除所有类添加messagebox类并开始淡入淡出
$(“#msgbox”).removeClass().addClass('messagebox').text('Checking…').fadeIn(“slow”);
//从ajax检查用户名是否存在
$.post(“user_availability.php”,{username:$(this.val()},函数(数据)
{
if(data='no')//如果用户名不可用
{
$(“#msgbox”).fadeTo(200,0.1,function()//开始淡入messagebox
{
//添加消息并更改框的类别,然后开始淡入淡出
$(this).html('Username not available to register').addClass('messageboxerror').fadeTo(900,1);
});
}
其他的
{
$(“#msgbox”).fadeTo(200,0.1,function()//开始淡入messagebox
{
//添加消息并更改框的类别,然后开始淡入淡出
$(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);
});
}
});
});
});
将您的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";
//连接到MYSQL数据库//
$host='localhost';
$user='root';
$pass='root';
$db=‘表’;
$connect=mysql\u connect($host,$user,$pass)或die(“无法连接到mysql!”);
mysql\u set\u字符集('utf8',$connect);
mysql_select_db($db)或die(“找不到数据库”);
//AJAX发送的表单值
$user\u name=$\u POST['username'];
$length=strlen($user\u name);
如果($长度<5){
回显“否”;退出();
}
//抓取数据库中的所有用户
$query=“从用户中选择用户名”;
$result=mysql\u query($query);
$count=mysql\u num\u行($result)$i=0;
而($i<$count){
$existing_user=mysql_result($result,$i,'username');
如果($user\u name==$existing\u user)
{
退出();
}
$i++;
}
//没有匹配项,因此返回OK以继续使用name
回应“是”;
这是一个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>
$(文档).ready(函数(){
$(“#findname”).blur(函数()
{
//删除所有类添加messagebox类并开始淡入淡出
$(“#msgbox”).removeClass().addClass('messagebox').text('Checking…').fadeIn(“slow”);
//从ajax检查用户名是否存在
$.post(“user_availability.php”,{username:$(this.val()},函数(数据)
{
if(data='no')//如果用户名不可用
{
$(“#msgbox”).fadeTo(200,0.1,function()//开始淡入messagebox
{
//添加消息并更改框的类别,然后开始淡入淡出
$(this).html('Username not available to register').addClass('messageboxerror').fadeTo(900,1);
});
}
其他的
{
$(“#msgbox”).fadeTo(200,0.1,function()//开始淡入messagebox
{
//添加消息并更改框的类别,然后开始淡入淡出
$(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);
});
}
});
});
});
将您的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";
//连接到MYSQL数据库//
$host='localhost';
$user='root';
$pass='root';
$db=‘表’;
$connect=mysql\u connect($host,$user,$pass)或die(“无法连接到mysql!”);
mysql\u set\u字符集('utf8',$connect);
mysql_select_db($db)或die(“找不到数据库”);
//AJAX发送的表单值
$user\u name=$\u POST['username'];
$length=strlen($user\u name);
如果($长度<5){
回显“否”;退出();
}
//抢占一切
$date = $_GET['date'];
$query = "SELECT * FROM WHERE date = " . $date;