Hello,

I posted here as although it's in regards to mysql the thing i want to ask is in regards to php itself.

I have two tables below as follows:

CREATE TABLE `users` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`status` char(10) NOT NULL,
`username` varchar(15) NOT NULL,
`email` varchar(50) NOT NULL,
`password` char(32) NOT NULL,
`reg_date` int(11) NOT NULL,
`ip` varchar(39) DEFAULT NULL,
PRIMARY KEY (`uid`),
UNIQUE KEY `username` (`username`,`email`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;


CREATE TABLE `users_profiles` (
  `uid` int(11) NOT NULL,
  `first_name` varchar(40) DEFAULT NULL,
  `last_name` varchar(50) DEFAULT NULL,
  `gender` char(6) DEFAULT NULL,
  `website` varchar(100) DEFAULT NULL,
  `msn` varchar(60) DEFAULT NULL,
  `aim` varchar(60) DEFAULT NULL,
  `yim` varchar(60) DEFAULT NULL,
  `twitter` varchar(15) DEFAULT NULL,
  UNIQUE KEY `uid` (`uid`),
  CONSTRAINT `users_profiles_ibfk_1` FOREIGN KEY (`uid`) REFERENCES `users` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

As you can see the users_profiles table has a foreign key on the users_profiles.uid column and is referenced to the uid in the users table

My scenario is this:

A user fills out a registration form that asks for username, email and a password. After validating the form data etc it is inserted into the users table.

Now at this stage they have no profile in the users_profiles table as they only signed up. They login and xxx data is stored in a session along with there uid from users table. They now visit there profile, they fill in the profile form fields and submit the form.

Now can someone tell me if what i think i should be doing is correct, if not please tell me what i should do or a better way:

After validating the data that was submitted on profile form etc i could do something like a simple if else statement which will first check to see if the uid in session which is from users table matched a uid in the users_profiles table. If there's no match which in this case it won't be as they are a new user and don't have a profile record in users_profiles table then do an INSERT query into users_profiles table which would insert the submitted values from the form and the uid from session, on success that would now link the user from users table to there newly created profile in the users_profiles table. But if the uid in session matched a uid in user_profiles then obviously this means the user already has a profile ie.. submitted profile info previously so do an UPDATE query instead.

Am i rite in thinking this is how i would do it?

example below, note it's not valid code just to try an illustrate what i am saying; Hopefully!

if($_SESSION['uid'] == user_profiles.uid)
{
     /* If the uid in session which is the uid from users table 
      * (we got it on successful login)
      * matches a users uid in user_profiles table then profile already exists
      * for user so do an UPDATE query here
     */
} else {
    /* $_SESSION['uid'] does not match a uid in users_profiles table
     * so instead do a INSERT QUERY here. This means no profile exists for user.
     * The uid from session is inserted into users_profiles table (column uid) 
     * which links the profile to user.
     */
}

I know if i delete a user it will also delete there profile if they have one like it should do to obviously not leave redundant data in the database and to ensure data intergity (sorry i think that's what it's called)

Thanks for any help.

PHPLOVER

Yup..
Your logic is absolutely right. :)

Thanks vibhadevit :)

I know i went on to long but struggle to explain things in little details, so thanks for reading it all. Just wanted to ensure i was on the rite path and making sure there was no better way.

Cheers! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.