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开发者_Go百科 FROM employee";
$result = queryMysql($query);
if (Classification_ClassificationID =='2') {
while ($rows = mysqli_fetch_array($result)) {
<select name = "UserName" value="' . $rows['UserName'] . '" name="toinsert[]" />;
}
}
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.
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.
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>
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>";
}
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.
精彩评论