0

嗨,谢谢你和我一起看这个。我对使用 PHP 运行 MySQL 选择语句完全陌生。话虽如此,我已经成功地运行了一个 SELECT 语句来填充一个下拉列表......以及另一个 SELECT 语句来填充一个 HTML 表。(这是一个角色扮演游戏)

但这就是我卡住的地方......

我希望下拉选择的值是填充表的第二个选择语句中的“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>"; 

?> 

我希望这很简单......非常感谢:)

~ 杰克

4

1 回答 1

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>"; 
}
?>

我修改了你的代码并添加了一些东西让你开始,如果在尝试加载我写的页面时有任何错误,请原谅我没有尝试它

祝你好运!

于 2012-08-01T14:28:13.280 回答