MY CODE:
// variables
$user_id = $_SESSION['user_ID']; // session ID
// query db for user info
$get_info = $conn->prepare("SELECT users.user_id, users.first_name,
users.last_name,
users.country_origin,
users.looking_for,
users.exp_yrs,
users_skills.user_id,
users_skills.user_skill
FROM `users`
LEFT JOIN `users_skills`
ON users.user_id = ?
AND users_skills.user_id = users.user_id
$get_info->bind_param('s',$user_id);
$get_info->execute();
$row_user_details = $get_info->get_result()->fetch_Assoc(); // get result
<div class="col"><label class="input-filled"><input name="first_name" required value="<?php echo $row_user_details['first_name']; ?>">
<div class="col"><label class="input-filled"><input name="last_name" required value="<?php echo $row_user_details['last_name']; ?>">
While(" i can't use $row_user_details = $get_info->fetch_Assoc()"){
<div class="col"><label class="input-filled"><input name="last_name" required value="<?php echo $row_user_details['user_skill']; ?>">
}
i am not sure what to put in the while () cuz i need to use the values of the query outside of the while loop aswell as you can see from the code, i have been at it for 1-2 hrs and just can't figure it out
user_skill is multiple data in one table that has the same user_id
where as first name and last name is only one value from the user row
DB layout:
users_skills TABLE:
ID | user_id | user_skill |
1 | 1 | something1 |
2 | 1 | something2 |
3 | 1 | something3 |
4 | 2 | somethine4 |
5 | 6 | somethine5 |
users TABLE:
user_id | first_name | last_name | some more stuff |
1 | john | key | secrets |
the session ID is = user_id // in this case 1
This code has been shorten so not to reveal info to the public but it works the same
My Answer:
To fetch multiple rows of user skills for a specific user ID, you can use a while loop to iterate over the results. Here's how you can modify your code:
php
// variables
$user_id = $_SESSION['user_ID']; // session ID
// query db for user info
$get_info = $conn->prepare("SELECT users.user_id, users.first_name,
users.last_name,
users.country_origin,
users.looking_for,
users.exp_yrs,
users_skills.user_id,
users_skills.user_skill
FROM `users`
LEFT JOIN `users_skills`
ON users.user_id = ?
AND users_skills.user_id = users.user_id");
$get_info->bind_param('s', $user_id);
$get_info->execute();
$result = $get_info->get_result();
$row_user_details = $result->fetch_assoc(); // get user details
// Display user details
// Display user skills using a while loop
while($row_user_skill = $result->fetch_assoc()){
}
In this code snippet, we fetch the user details outside the while loop and then use a while loop to fetch and display the user skills. The user skills are displayed inside the while loop, and each skill is added to an input field with the name "user_skill[]". This will allow you to capture multiple user skills in an array when the form is submitted.
Rate this post
5 of 5 based on 7042 votesComments