chrisgibson Posted November 26, 2007 Share Posted November 26, 2007 Ok I am completely new to PHP and literally started using it about a week ago. I have searched and searched on google about this and everything I read goes way over my head. I am doing a project for a class where we are setting up a LAMP server. Then we are to make a table in MySQL and use a PHP page to access the table in the database and display information for different group members. I have made the table and made an index.php page and was able to get a query to the database and print the results when I hardcoded the SQL statement. However I was wanting to be able to use a drop down list with the names of my group members(hard coded) and be able to place that name in the SQL statement by hitting a submit button. This is where I am stuck. I have made a list box by adding the following HTML to the page: <select name="Members"> <option>Chris Gibson</option> <option>Nick Carden</option> <option>Jake Johnson</option> </select> Then I make a database connection to MySQL with the following: <?php $dbhost = '192.168.1.127' $dbuser = 'root' $dbpass = 'password' $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to MySQL'); $dbname = 'groupmembers'; mysql_select_db($dbname); $query = mysql_query("SELECT * from members WHERE name=Chris Gibson");<-I would like for "WHERE name=$someVariable" to be dynamic and $someVariable to be obtained from the drop down list $info = mysql_fetch_array($query); echo $info['name'] . "<br>": echo $info['address'] . "<br>": echo $info['city'] . "<br>": echo $info['state'] . "<br>": echo $info['zip'] . "<br>": echo $info['phone'] . "<br>": mysql_close($conn) ?> When I use that code it will print out my information stored in the members table of the groupmembers database. So essentially what I need to be able to do is select the name of a member from a dropdown list. Place that name in a variable or direct the SQL statement to the drop down list value that is selected. Any help will be greatly appreciated. Thanks, Chris Quote Link to comment Share on other sites More sharing options...
Wes1890 Posted November 26, 2007 Share Posted November 26, 2007 Change your HTML: <select name="Members"> <option value="Chris">Chris Gibson</option> <option value="Nick">Nick Carden</option> <option value="Jake">Jake Johnson</option> </select> So if we selected "Chris Gibson", the php variable $_POST['members'] would be "Chris". Also, you are using the dropdown menu in a form right? And are you getting the $_POST or $_GET vars? I must ask this because I cant see in the code you posted. Quote Link to comment Share on other sites More sharing options...
revraz Posted November 26, 2007 Share Posted November 26, 2007 You're missing the value= in your Options. Also, try to work your code where the PHP is on top and the HTML is after your code. The "some variable" for your sql query will be the value of the Members Select. Quote Link to comment Share on other sites More sharing options...
chrisgibson Posted November 26, 2007 Author Share Posted November 26, 2007 I am not using it in a form and I am guessing from your post that I should be. That is also new to me and I hate to ask how to do that but could you elaborate on that a bit more? I will go and change the HTML and put it under neath the php code Quote Link to comment Share on other sites More sharing options...
Wes1890 Posted November 26, 2007 Share Posted November 26, 2007 Here's a short tutorial on this http://www.tizag.com/phpT/forms.php Quote Link to comment Share on other sites More sharing options...
revraz Posted November 26, 2007 Share Posted November 26, 2007 Without a form, how would you pass the variable? I am not using it in a form and I am guessing from your post that I should be. That is also new to me and I hate to ask how to do that but could you elaborate on that a bit more? I will go and change the HTML and put it under neath the php code Quote Link to comment Share on other sites More sharing options...
chrisgibson Posted November 26, 2007 Author Share Posted November 26, 2007 Ok the HTML now looks like this and it is underneath the PHP code: <form action="index.php" method="post"> <select name="members"> <option value="Chris">Chris Gibson</option> <option value="Nick">Nick Carden</option> <option value="Jake>Jake Johnson</option> </select> <input type=submit /> <--I added this for the submit button </form> I also added the following PHP code under the line where it sets the password ($dbpass = 'password': $name = $_POST['members']; I read that tutorial and it makes more sense than it did. Tell me if this is what I should have gotten from that tutorial. We need a form to take in the user inputs and send the inputs to a processing page? Quote Link to comment Share on other sites More sharing options...
chrisgibson Posted November 27, 2007 Author Share Posted November 27, 2007 I ended up up changing the values in the <option> tag to the full name of the group member and it fixed it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.