嗨,谢谢你和我一起看这个。我完全是使用PHP运行MySQL select语句的新手。话虽这么说,我已经设法运行一个SELECT语句来填充下拉列表...和另一个SELECT语句来填充HTML表。 (这是一个角色扮演游戏)
但这就是我被困的地方......
我希望下拉选择值为填充表的第二个select语句中的“WHERE racename =”值,以便只返回一行而不是所有数据。
以下是页面:http://www.gamehermit.com/racechoice.php
到目前为止,这是我的代码:
<?php
// Make a MySQL Connection
mysql_connect("localhost", "db_username", "password") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());
$query="SELECT * FROM Races";
$result = mysql_query($query);
echo "<select name=racename>";
while($nt=mysql_fetch_array($result))
{
if ($nt[racename]==$_POST["racename"])
$selected="selected";
else
$selected="";
echo "<option ".$selected."value=$nt[racename]>$nt[racename]</option>";
}
echo "</select>";
echo "<br />";
// Get all the data from the "Race" table and create table
$result2 = mysql_query("SELECT * FROM Races")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Race Name</th> <th>Might Modifier</th> <th>Valor Modifier</th> <th>Deftness
Modifier</th> <th>Insight Modifier</th> <th>Dweomer Modifier</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result2 )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['racename'];
echo "</td><td>";
echo $row['modmight'];
echo "</td><td>";
echo $row['modvalor'];
echo "</td><td>";
echo $row['moddeftness'];
echo "</td><td>";
echo $row['modinsight'];
echo "</td><td>";
echo $row['moddweomer'];
echo "</td></tr>";
}
echo "</table>";
?>
我希望这很简单......非常感谢:)
〜杰克
答案 0 :(得分:0)
最好的方法是使用 AJAX ,这样您就不需要传递变量并加载新页面。
但是你可以用老式的方式来做到这一点:
假设您只有一个页面,并且您将所选值传递到同一页面(需要重新加载页面)
所以我们说你的网页是game.php
你需要在这个页面中包含一个“跳转菜单”,当用户从列表中选择一些内容后,按下提交按钮
在页面标题中,您需要使用
检查按钮是否被按下if(isset($_POST['button_name'])) {
// button pressed.. perform next step and select your new data to fill the table
} else {
// nothing pressed and nothing to be performed load the page normally
}
在“if”的“true”里面你需要从列表中获取传递的变量,例如
$var = $_POST['list_name'];
所以现在你有了第二个变量来选择填充表格所需的数据。
完整的代码应该类似于以下内容,game.php:
<?php
if(!isset($_POST['go_button'])){ //option not selected display list to choose from
// Make a MySQL Connection
mysql_connect("localhost", "db_username", "password") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());
$query="SELECT * FROM Races";
$result = mysql_query($query);
$num = mysql_numrows($result);
?>
<script type="text/javascript">
function MM_jumpMenuGo(objId,targ,restore){ //v9.0
var selObj = null; with (document) {
if (getElementById) selObj = getElementById(objId);
if (selObj) eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0; }
}
</script>
<form name="form" id="form" action="game.php" method="post">
<select name="jumpMenu" id="jumpMenu">
<?php $i=0; while($i<$num) { ?>
<option value="<?php echo mysql_result($result,$i,'racename_field_value'); ?>"><?php echo mysql_result($result,$i,'racename'); ?></option>
<?php } ?>
</select>
<input type="button" name="go_button" id= "go_button" value="Go" onClick="MM_jumpMenuGo('jumpMenu','parent',0)">
</form>
<?php
echo "<br />";
} else { //option selected to get the variable and use it to select data from DB
$var= $_POST['jumpMenu'];
// Get all the data from the "Race" table and create table
$result2 = mysql_query("SELECT * FROM Races WHERE racename='$var'")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Race Name</th> <th>Might Modifier</th> <th>Valor Modifier</th> <th>Deftness
Modifier</th> <th>Insight Modifier</th> <th>Dweomer Modifier</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result2 )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['racename'];
echo "</td><td>";
echo $row['modmight'];
echo "</td><td>";
echo $row['modvalor'];
echo "</td><td>";
echo $row['moddeftness'];
echo "</td><td>";
echo $row['modinsight'];
echo "</td><td>";
echo $row['moddweomer'];
echo "</td></tr>";
}
echo "</table>";
}
?>
我修改了你的代码,并添加了一些东西让你开始,对不起,如果在尝试加载我写的页面时没有尝试而有任何错误
祝你好运!