0

Im new in PHP. I need to create a form that display a dropdown list that show the employees names that I have in a mysql table called employee. After have this dropdown list. I need that the select value from the dropdown list be inserted in another table. Here is what I have. I really apreciate your help.

echo '</tr><tr><td><label for="AssignedEmp"> Assigned Employee:</label></td><td>';
$query = "SELECT UserName, Classification_ClassificationID FROM employee";
$result = queryMysql($query);
if (Classification_ClassificationID =='2')  {
   while ($rows = mysqli_fetch_array($result)) {
      <select name = "UserName" value="' . $rows['UserName'] . '" name="toinsert[]" />;
    }
 }
1
  • 1
    What, specifically, is your question? Have you tried this code out and something is not working? Commented Dec 29, 2010 at 15:58

5 Answers 5

3

OK, a few things to go over.

1. First things first, if you only want the classification id to be 2, make the database do it:

SELECT UserName, Classification_ClassificationID FROM employee WHERE Classification_ClassificationID = 2

2. Next, I'm not sure you've quite grasped how html <select> works: http://www.w3schools.com/tags/tag_select.asp

You're going to want the output to look something like this:

<select name="UserName">
  <option value="BillyBob">BillyBob</option>
  <option value="JonSmith">JonSmith</option
  <option value="BunnyRabbit">BunnyRabbit</option>
</select>

So the PHP to generate it would be:

echo "<select name=\"UserName\">";

while ($rows = mysql_fetch_array($result)) {
  echo "<option value=\"$rows[UserName]\">$rows[UserName]</option>";
}

echo "</select>";

3. Make sure you echo the html from php, or it's not going to work.

Sign up to request clarification or add additional context in comments.

Comments

0
if (Classification_ClassificationID =='2')  {
   while ($rows = mysqli_fetch_array($result)) {
      <select name = "UserName" value="' . $rows['UserName'] . '" name="toinsert[]" />;
    }
 }

This is invalid. Need to echo that select line.

Comments

0

You need to fetch the info before you can do the check.

The

if (Classification_ClassificationID =='2'){

Part will not evaluate b/c it's not set. You'd have to check it like...

if ($row['Classification_ClassificationID'] =='2'){

And do this inside the while statement. I think you want something like this...

NOTE: I added the check for ==2 into your SQL statement

<?php
$query = "SELECT UserName, Classification_ClassificationID FROM employee WHERE Classification_ClassificationID=2";
$result = queryMysql($query);
?>
<select name="UserName">
<?php
while ($rows = mysqli_fetch_array($result)) {
?>
   <option value="<?php echo $rows['UserName']?>" />
<?php
} // close the while
?>
</select>

Comments

0

A html select box has this format

<select name="foo">
  <option value="1">1</option>
  <option value="2">2</option>
</select>

so your php should look like this

..
if (Classification_ClassificationID =='2'){
  echo "<select name=\"UserName\">";
  while ($rows = mysqli_fetch_array($result)) {
    echo "<option value=\"". $rows['UserName'] ."\">". $rows['UserName'] ."</option>";
  }
  echo "</select>";
}

Comments

0

Try :

echo '</tr><tr><td><label for="AssignedEmp"> Assigned Employee:</label></td><td>';
$query = "SELECT UserName FROM employee where Classification_ClassificationID = '2'"; // First Remark
$result = mysqli_query($query); 
echo '<select name = "UserName">'; // or name="toinsert[]" 
while ($rows = mysqli_fetch_array($result)) {
  echo '<option value="' . $rows['UserName'] . '" />'; // Third remark
}
echo '</select>';

First remark : Make the correct request to begin with. Here you select every userName in the employee table where Classification_ClassificationID = '2' (put only 2 if the field is not a varchar)

Second remark : Be sure you're really using mysqli (or mysql?)

Third remark : Be sure to place an echo before something you want to write as output (here, in html)

Finally : Before learning PHP, you may want to learn HTML first.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.