John Davidson

php - Select only shows last name

0 comments
Message:


I have a form where you need to be able to select the mentor, and add a name. The selecting a mentor is working fine, apart from one part: It only shows the last name ('achternaam') and it needs to show the first name ('voornaam') too. What did I do wrong?


<form action="index.php" method="post">
<p>
<label for="mentor">Mentor:</label>
<select name="mentor" id="mentor">
<?php

if ($result->num_rows > 0) {
//output data of each row
while ($mentors = $result->fetch_assoc()) {
echo '<option value="' . $$mentors['voornaam'] . '">' . $mentors['achternaam'] . '</option>';
}
} else {
echo "0 results";
}

?>
</select> <br>

<label for="klasnaam">Klasnaam:</label>
<input type="text" id="klasnaam" name="klasnaam"> <br>
<input type="hidden" name="action" value="addClass" />
<input type="submit" value="Submit">

<?php


?>

</p>
</form>


My Answer:
It seems like you are only fetching and displaying the last name ('achternaam') in your select options. To display both the first name ('voornaam') and last name ('achternaam'), you need to modify your query to fetch both columns and concatenate them in the option value.

Here is an example of how you can modify your query and select options:

1. Modify your query to fetch both first name and last name:

php
$query = "SELECT mentor_id, CONCAT(voornaam, ' ', achternaam) AS mentor_name FROM mentors";
$result = mysqli_query($connection, $query);


2. Modify your select options to display both first name and last name:

html



By concatenating the first name and last name in the query and displaying it in the select options, you should now see both the first name and last name when selecting a mentor.

Rate this post

5 of 5 based on 4657 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us