PHP and SQL - Relational Databases

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

PHP and SQL - Relational Databases

Post by timWebUK »

I'm still working on implementing my database design that I've mentioned in a few other threads lately and I'm running into a few questions as I go that I can't seem to get a straight answer on, but i've figured a decent way to word them - hoping someone can provide me with a decent answer!

I have 5 tables:

tblProject
tblDwelling
tblDoorType
tblWallType
tblWindowType

A user creates a new project, that project can have one dwelling associated with it. Each dwelling can be made up of multiple door types, wall types and window types. Each table obviously has a unique ID. The dwelling also has a project ID as an FK, door/window and wall each have a dwelling ID as an FK.

What is the best way to implement this in PHP? I have created a form to create a new project and generate an ID and project name - how do I go about taking that ID, storing it (as a foreign key) in the dwelling and creating a new dwelling. Same goes for each indidual window/door/wall. I really am struggling to get my head around this andwould appreciate any help! Do i need to run a query to grab the project and dwelling ID and store in a session or what? Extremely confused here.

Cheers.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: PHP and SQL - Relational Databases

Post by Eran »

Passing a project / dwelling ID is no different than any other value in the form. Either you allow the user to select it using a SELECT dropdown or text input, or you put it as an hidden input (pass it via the URL or any other similar method)
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: PHP and SQL - Relational Databases

Post by timWebUK »

But then we have an issue where this ID could then be changed... how can I validate this?

Currently I am using an auto-incrementing ID for primary keys, so it hasn't needed to be specified in a form.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: PHP and SQL - Relational Databases

Post by Eran »

You misunderstood me - you pass the project ID in a dwelling form, the dwelling ID in a window form etc. Those are still auto-incremented IDs in their original tables. The foreign keys obviously are not auto-incremented numbers, they have to be specified when creating a row
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: PHP and SQL - Relational Databases

Post by timWebUK »

I'll go ahead and try that! I've just discovered this function too:

http://www.php.net/manual/en/function.m ... ert-id.php
Post Reply