Jump to content

Passing dropdown list value to another SELECT statement on same page


GameHermit

Recommended Posts

Hi and thanks for looking at this with me. I am COMPLETELY new to using PHP to run MySQL select statements. That being said, I have managed to run a SELECT statement to populate a drop down list...and another SELECT statement to populate an HTML table. (this is for a roleplaying game)

 

But this is where3 I get stuck...

 

I would like for the dropdown selected value to be the "WHERE racename = " value in the second select statement that populates the table so that only one row is returned instead of all the data.

 

Here's the page: [gamehermit.com...]

 

Here's my code so far:

 

 

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

?> 

 

 

I hope this is simple...thanks so much :)

 

~ Jack

Link to comment
Share on other sites

Thanks Christian. That's a great document :) I'm a complete noob but I'm learning...but I have a quick question.

 

Here's what I have setup now:

 

racechoice.php grabs the value from the dropdown and posts it to the action.php. What I'm not sure about is what the actual value is called that is being passed from the dropdown (so I don't know how to structure the query on the action.php.

 

Here's the dropdown code on the first page:

 

<?php

// Make a MySQL Connection

mysql_connect("localhost", "user_name", "password") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());
   
echo "<form action='action.php' method='post'>";

    $query="SELECT * FROM Races";
    $result = mysql_query($query);
    echo "<select name=racechoice>";
    while($nt=mysql_fetch_array($result))
    {
    if ($nt[racename]==$_POST[raceselected])
       $selected="selected";
    else
        $selected="";
    echo "<option ".$selected."value=$nt[raceselected]>$nt[racename]</option>";
    }
    echo "</select>";
    echo "<br />";
echo "<p><input type='submit' /></p>";
echo "</form>";
?>

 

and here's the action.php (this is where I'm having trouble structuring the SELECT statement because I don't know how to call the value from the dropdown that the first page passes):

 

<?php

// Make a MySQL Connection

mysql_connect("localhost", "user_name", "password") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());

// Get all the data from the "Race" table and create table

$result2 = mysql_query("SELECT * FROM Races WHERE racename = THIS IS WHAT I DON'T KNOW HOW TO SET") 
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>";
    
?>

 

 

Link to comment
Share on other sites

The data you're looking for is in the $_POST array. So I recommend running a echo '<pre>';print_r ($_POST);die (); at the top of the "action.php" page, so that you can see for yourself what it contains. What you need to do then, should be fairly obvious.

Just remember to validate input, and escape output. The latter is done with mysql_real_escape_string (), and only as the last action before you add the variable's content to the query.

 

As for the PHP manual, it is indeed an excellent document. Most of the time you'll find the answer to any question you have there. This includes the current one, btw. :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.