PHP+MySQL实现下拉框显示数据库信息

WBOY
Freigeben: 2016-06-23 13:44:26
Original
2085 Leute haben es durchsucht



  Classroom research



  

CRMS - Classroom research



  

    
      
        
         
      
      
  
  
      

      
        
      
    
Classroom ID
Course ID

        

  




1、我希望在第一个框输入课室号,第二框通过下拉框显示数据库中course2表的CID的内容。
这应该就是不对的,毫无头绪,不知道怎么实现。求详细代码!
2、怎样实现填写其中一个数据(即不用填两个)就可以查询数据?


回复讨论(解决方案)

什么意思。下拉框选中input框的值?

下拉框中出现数据库crms中course2表的CID的内容

while($row = mysql_fetch_array($result))
  {
    echo "";
  }

不行。我也不不知道在select中间插入一段PHP的代码可不可以,这个只是我自己试一下而已

只要是php文件就行。允许html与php嵌套。

这得用AJAX了, 菜单联动就是了.

木有学过,能不能直接给代码我...应该不会很复杂吧

5楼的朋友,那应该怎么写啊?

你的代码加上#3修改的部分就行。不知道你遇到什么问题。

while($row = mysql_fetch_array($result))
  {
  
  }
 
把这一段改成

while($row = mysql_fetch_array($result))
  {
  echo "";
  }
这样是吧?
我改了,但是我的下拉框里还是没有出现CID的内容,只有请选择!

确认表里有数据。
$result = mysql_query($search_course, $con) or die(mysql_error());
这样有不有报错?

有数据。
没有报错,确认是用echo,这个不是只是回显在屏幕上吗?

仅供参考:

<?php/* Created on [2012-5-16] */#查询标题信息$sql="select * from table";	$res=mysql_query($sql);	if(!$res) die("SQL: {$sql} <br>Error:".mysql_error());	if(mysql_affected_rows() > 0){		$titles = array();		while($rows = mysql_fetch_array(MYSQL_ASSOC)){			array_push($titles,$rows);		}	}?><table border=1><?php foreach($titles as $row_Recordset_task){ ?>	<tr>		<td>			<a href="javascript:void(0)" onclick="record(<?=$row_Recordset_task['TID']?>)" >				<?=$row_Recordset_task['csa_title']?>			</a>		</td>	</tr><?php } ?></table><div id="show"></div><form name="frm"><select name="s1" onChange="redirec(this.value)"> <option selected>请选择</option> <option value="1">内科</option> <option value="2">内科</option> <option value="3">内科</option></select><div id="s2"></div></form><script>//Ajaxvar xmlHttp;	function createXMLHttpRequest() {		if(window.XMLHttpRequest) {			xmlHttp = new XMLHttpRequest();		} else if (window.ActiveXObject) {			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");		}	}	function record(id){		createXMLHttpRequest();		url = "action.php?id="+id+"&ran="+Math.random();		method = "GET";		xmlHttp.open(method,url,true);		xmlHttp.onreadystatechange = show;		xmlHttp.send(null);	}	function show(){		if (xmlHttp.readyState == 4){			if (xmlHttp.status == 200){				var text = xmlHttp.responseText;				document.getElementById("s2").innerHTML = text;			}else {				alert("response error code:"+xmlHttp.status);			}		}	}</script><?php#action.phpif(isset($_GET['id'])){	$sql="select * from table where id=".$_GET['id'];	$res=mysql_query($sql);	if(!$res) die("SQL: {$sql} <br>Error:".mysql_error());	if(mysql_affected_rows() > 0){		$arrMenu=array();		while($rows = mysql_fetch_array(MYSQL_ASSOC)){			array_push($arrMenu,$rows);		}	}	mysql_close();	if(!empty($arrMenu)){		echo "<select name='menu2'>";		foreach($arrMenu as $item2){			echo "<option value='{$item2['id']}'>{$item2['name']}</option>";		}		echo "</select>";	}}?>
Nach dem Login kopieren

就这么简单,不需要上面那么复杂!!?直接复制过去就可以用了。。。。

//require_once('conn.php'); //?最好写个连接数据库的文件 每次包含一下就行了! 而且要写在最上面

$con = mysql_connect("localhost","root","***");
?>




无标题文档






就这么简单,不需要上面那么复杂!!?直接复制过去就可以用了。。。。

//require_once('conn.php'); //?最好写个连接数据库的文件 每次包含一下就行了! 而且要写在最上面

$con = mysql_connect("localhost","root","***");
?>

换成

<option value="<?php echo $row['CID'] ?>"><?php echo $row['CID'] ?></option> 
Nach dem Login kopieren

这样?提交之后也能把值传过去!! 记得给

就这么简单,不需要上面那么复杂!!?直接复制过去就可以用了。。。。

//require_once('conn.php'); //?最好写个连接数据库的文件 每次包含一下就行了! 而且要写在最上面

//最好把下面三行写在conn.php文件里 以后每次用时 向上面那样包含一下就OK了!!
$con = mysql_connect("localhost","root","***") or die("错误信息:".mysql_error()); //连接
$db = mysql_select_db("表course2所在的数据库名"); //这个要不写就取不着数据 但不会报错
mysql_query("set names gb2312");

?>




无标题文档





你好,因为我才刚刚开始学,对于HTML和PHP之间的转换很不了解。我已经在一个HTML里面写好head,已经在写body了(上面还有一个框用来填课室号的,已实现),才开始写课程号(用你的方法),下面我还要写时间(已实现),那怎么在中间插入你的代码?因为你又重新开始写head了。

搞这么复杂?script标签在代码底部加也行啊,只要有:

<script>//Ajaxvar xmlHttp;    function createXMLHttpRequest() {        if(window.XMLHttpRequest) {            xmlHttp = new XMLHttpRequest();        } else if (window.ActiveXObject) {            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");        }    }    function record(id){        createXMLHttpRequest();//指定目标地址及参数        url = "action.php?id="+id+"&ran="+Math.random();        method = "GET";        xmlHttp.open(method,url,true);        xmlHttp.onreadystatechange = show;        xmlHttp.send(null);    }    function show(){        if (xmlHttp.readyState == 4){            if (xmlHttp.status == 200){//回调函数,返回的后端结果                var text = xmlHttp.responseText;                document.getElementById("s2").innerHTML = text;            }else {                alert("response error code:"+xmlHttp.status);            }        }    }</script>
Nach dem Login kopieren

能运行就行

$sql="select CID from course2";
$result=mysql_query($sql);
while($row=mysql_fetch_assoc($result)){
?>

 //这个值要用php的方法取出来
}

我也觉得要这样写,但是就是显示不出来啊!

你没连接表。。。。。

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!