machination Posted December 14, 2009 Share Posted December 14, 2009 Hey, I'm making a dating site to get some practice since I'm a beginner. I may have a bunch of questions while getting this finished. First one: I have 3 tables right now. "clients" - stores client information they filled out in a form "client_photos" - stores their photo "client_interests" - has each interest in a separate row All of these tables must have the same client_id (but it's only the primary key for the "clients" table; the other two have their own primary keys). I am trying to get the "interests" the user inputs separated by commas into the rows. How can I go about doing this properly? Here's an excerpt of that section: $insert_id = mysqli_insert_id($oConn); $query2 = "INSERT INTO client_interests(client_id, interest) ". "VALUES ('$insert_id', _____')"; $result2 = mysqli_query($oConn, $query2) or die('Error querying database.'); In the $query2 area (where I left an empty "______")...how can I write it out (simplest way) so that the interests (which the user has written out with commas in-between) is exploded into multiple rows? Something like this? $interest_list = implode(',', $interests); And/or an array? Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/185104-making-a-dating-site/ Share on other sites More sharing options...
machination Posted December 14, 2009 Author Share Posted December 14, 2009 Something like this maybe? $insert_id = mysqli_insert_id($oConn); $interest_list = implode(',', $interests); $query2 = "INSERT INTO client_interests(client_id, interest) ". "VALUES ('$insert_id', $interest_list')"; Quote Link to comment https://forums.phpfreaks.com/topic/185104-making-a-dating-site/#findComment-977136 Share on other sites More sharing options...
machination Posted December 16, 2009 Author Share Posted December 16, 2009 OK, I've figured out most of it, but I need some quick help with this last section. If someone could tell me the error, it would be appreciated. $matchClients = "SELECT COUNT(c2.interest) AS num_common_interests, c2.client_id AS matching_client, c1.client_id AS current_client FROM client_interests AS c1, client_interests AS c2 , clients AS c, clients AS mc WHERE c1.interest=c2.interest AND c1.client_id= " . $_SESSION['client_id'] ." AND c1.client_id <> c2.client_id AND c1.client_id = c.client_id AND c2.client_id = mc.client_id AND( c.desired_sex = mc.sex OR c.desired_sex = 'any' ) AND( mc.desired_sex = c.sex OR mc.desired_sex = 'any' ) GROUP BY c2.client_id, current_client ORDER BY num_common_interests DESC"; $clientRS = mysqli_query($oConn, $matchClients); if($clientRS){ while($rowC = mysqli_fetch_array($clientRS)){ //fetch the image from client_photos with a new query. $strSQL = "SELECT file_name FROM `client_photos` WHERE client_id = " .$rowC['client_id']; $clientPT = mysqli_query($oConn, $strSQL); if($clientPT){ $rowP= mysqli_fetch_array($clientPT); //fetch the image from client_photos with a new query. echo ''; echo '<div class="clientIndiv"><img src="./uploaded/' . $rowP['file_name'] . '" alt="photo" />'; echo '<span class="bold">' . "City:" . '</span>' . ' ' . $rowC['city'] . "<br/>"; echo '<span class="bold">' . "Age:" . '</span>' . ' ' . $rowC['dob'] . "</div>"; } } } I'm getting an undefined index client_id error for "WHERE client_id = " .$rowC['client_id'];" as well. Quote Link to comment https://forums.phpfreaks.com/topic/185104-making-a-dating-site/#findComment-978240 Share on other sites More sharing options...
JonnoTheDev Posted December 16, 2009 Share Posted December 16, 2009 I am trying to get the "interests" the user inputs separated by commas into the rows. How can I go about doing this properly? Here's an excerpt of that section: This is highly inefficient and not a normalised structure. You SHOULD have a table containing a list of interests, a table containing a list of users, and a table joining users to interests i.e users ====== userId name interests ========= interestId title usersToInterests ================ id userId interestId Lets take 1 record from users: userId: 1 name: Neil Lets see the interests available: 1 Football 2 Movies 3 TV 4 Computers 5 Golf OK, now lets relate user above to Football, Movies, and Golf: 1 1 1 2 1 2 3 1 5 You can see that userId 1 has the interests 1,2,5 Your query should join the corresponding tables Quote Link to comment https://forums.phpfreaks.com/topic/185104-making-a-dating-site/#findComment-978380 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.