Love2c0de Posted May 14, 2013 Share Posted May 14, 2013 Good evening, I am working on user profiles and wonder whether or not I should bother validating the facebook, twitter and skype fields? For example, checking the twitter name starts with the @ symbol etc. I want to know what some of you would do, I don't really want to display an error for a bad social media name but I'm not sure if it standard practice to do so. Simple question really but it will probably lead into more questions before the night is gone! Thanks for your help and advice. Kind regards, L2c. Quote Link to comment Share on other sites More sharing options...
Love2c0de Posted May 16, 2013 Author Share Posted May 16, 2013 (edited) Good evening all, I wanted to know something about users/profiles accounts. When a user registers should I be creating a default profile for them in my profiles table at the same that they register, maybe with null or empty fields? I hardcoded a dummy profile so I had something to work with and at the moment when they register, I only insert the data into the user table, and nothing into the profiles table. I am linking the two tables together via the username. With my dummy profile, I am at the point where I am now going to write the update query, but I figured if I don't create a default profile on registration, there will be no row to update as such. I thought if I insert the username into the profiles table at the same time as registering their details in my users table, when they come to 'editing' their profile and updating their profile, it will have a row to select. Is my train of thought correct or completely off mark? I hope I got my point across well enough. Kind regards, L2c. Edited May 16, 2013 by Love2c0de Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 16, 2013 Share Posted May 16, 2013 Why is a profile a separate table? Quote Link to comment Share on other sites More sharing options...
Love2c0de Posted May 16, 2013 Author Share Posted May 16, 2013 To be quite honest, I thought it was the correct way to do so, keep 'required' data if you like in my users table and anything trivial such as a profile which isn't necessary I wanted to keep in a separate table. I'm guessing that normally you would keep all this info within the same table? Kind regards, L2c. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 16, 2013 Share Posted May 16, 2013 If you want it in a separate table, no, don't create an empty row. Your code should handle inserting the new row if needed or editing the existing row. You should also use ids as your primary key, not username. Quote Link to comment Share on other sites More sharing options...
Love2c0de Posted May 16, 2013 Author Share Posted May 16, 2013 Thanks a lot, I'll start editing that now. Kind regards, L2c. Quote Link to comment Share on other sites More sharing options...
Love2c0de Posted May 18, 2013 Author Share Posted May 18, 2013 (edited) Good morning, I have now changed the code so that I login depending on the id's matching instead of the usernames. There seems to be a problem with my code though. I can login fine but I don't think my query is working correctly. The query should only join the profiles table onto the users table when the id's are matched depending on a session variable which I grab when they login. I only have one test profile setup in my DB at the minute but when I login to another test account, it still shows the 'template' div etc, where as it should be going into another php file which is going to insert their id and username into the profiles table, then run the SELECT query again to get their data. I'll post my code, it might be easier to understand. Here is my profile.tpl which is a template which I print into my main document: <?php if(!isset($_SESSION['user_name'])) { header("Location: ?page=home"); die(); } require("core/queries/profile_info.php"); if($row == 1) { $reg_date = date("l jS F Y H:i", $reg_date); $time_pos = strrpos($reg_date," "); $tmpstr = substr($reg_date,$time_pos); $tmpstr = " @".$tmpstr." hrs"; $reg_date = substr_replace($reg_date,$tmpstr,$time_pos); $last_login = date("l jS F Y H:i", $last_login); $time_pos = strrpos($last_login," "); $tmpstr = substr($last_login,$time_pos); $tmpstr = " @".$tmpstr." hrs"; $last_login = substr_replace($last_login,$tmpstr,$time_pos); //main profile information div $profile = "<div id='profile_main_box'>"; $profile .= "<div id='primary_info'>"; $profile .= "<h1 class='profile_user_name'>{$user1}</h1>"; $profile .= "<p class='profile_txt'>Member since: {$reg_date}</p>"; $profile .= "<p class='profile_txt'>Last logged in on: {$last_login}</p>"; $profile .= "</div>"; $profile .= "<button class='edit'>Edit Profile</button>"; //start form $profile .= "<form method='post' id='updateprofileform' action='?page=profile'>"; $profile .= "<fieldset><legend>Update your profile</legend>"; $profile .= "<p class='profile_p'><label for='fname'>First Name:</label><input type='text' name='fname' id='fname' value='{$fname}' /></p>"; $profile .= "<p class='profile_p'><label for='lname'>Last Name:</label><input type='text' name='lname' id='lname' value='{$lname}' /></p>"; $profile .= "<p><label for='dob'>Date of Birth:</label><input type='text' name='dob' id='dob' value='{$dob}' /></p>"; $profile .= "<p><label for='location'>Location:</label><input type='text' name='location' id='location' value='{$place}' /></p>"; if($sex == "Male") { $profile .= "<p class='radio'><label for='gender'>Gender:</label><input type='radio' name='gender' id='gender' value='male' />Male"; $profile .= "<input type='radio' name='gender' id='gender' value='female' /></p>"; } else { $profile .= "<p class='radio'><label for='gender'>Gender:</label><input type='radio' name='gender' id='gender' value='male' />"; $profile .= "<input type='radio' name='gender' id='gender' value='female' />Female</p>"; } $profile .= "<p><label for='fb'><img src='images/facebook_icon.png' class='form_img' alt='fb' />Facebook:</label><input type='text' name='fb' id='fb' value='{$fb}' /></p>"; $profile .= "<p><label for='twitter'><img src='images/twitter_icon.png' class='form_img' alt='twitter' />Twitter:</label><input type='text' name='twitter' id='twitter' value='{$twitter}' /></p>"; $profile .= "<p><label for='skype'><img src='images/skype_icon.png' class='form_img' alt='skype' />Skype:</label><input type='text' name='skype' id='skype' value='{$skype}' /></p>"; $profile .= "</fieldset>"; $profile .= "<input type='submit' value='Save Profile' />"; $profile .= "</form>"; $profile .= "</div>"; } else { //if the query failed, the user has not created a profile yet, so insert his id and username into the profiles table //then, run the profile_info query again to return the users profile to use, then they can update from there... require("core/queries/setup_profile.php"); } print($profile); ?> So as you can see, I click my profile link and I go into another file called profile_info.php which is here: <?php $conn = new mysqli("localhost","root","","*****") or die("Error creating connection."); $stmt = $conn->prepare("SELECT * FROM users LEFT JOIN profiles ON users.id=profiles.p_id WHERE users.id=?") or die("Error: ".mysqli_error($conn)); $stmt->bind_param("i",$_SESSION['id']); $stmt->execute(); $stmt->bind_result($id1,$user1,$pass,$email,$reg_date,$last_login,$id2,$user2,$fname,$lname,$dob,$sex,$place,$skype,$fb,$twitter); $stmt->store_result(); $stmt->fetch(); $row = $stmt->num_rows; mysqli_close($conn); ?> So the query is searching for id's to match and joins some data when it finds it. I presumed if it didn't find it, it would not return any 'num_rows'. When we return to the profile_info.php script, the if statement is executing ALWAYS, even when there is no ID which can be possibly matched. My idea is using the else statement to insert an id and username into profiles, only if they haven't previously setup a profile. I'm not a fan of concatenating too many times so I might put the form into another file and use file_get_contents() or something but for the time being just need to get this sorted. Iit must be the query. Can anyone possibly help? Kind regards, L2c. Edited May 18, 2013 by Love2c0de Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 18, 2013 Share Posted May 18, 2013 Do an inner join instead of left. Quote Link to comment Share on other sites More sharing options...
Love2c0de Posted May 18, 2013 Author Share Posted May 18, 2013 Fantastic that works perfectly now. Kind regards, L2c. Quote Link to comment 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.