Jump to content

[SOLVED] New to PHP and need help with a simple project


chrisgibson

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

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.