tags:

views:

25

answers:

2

Hello,

What's the correct way to implement the below concept via MySQL and PHP? Say a website has two groups/communities, both of which allow its users to ask Questions, answer Questions, and start Discussions, pertaining to the respective community. A user will choose which community he/she wishes to enter via below code(main.php):

<h2><a href="http://www.domain.com/groupA.php"&gt;Group A</a></h2>

<h2><a href="http://www.domain.com/groupB.php"&gt;Group B</a></h2>

After the user clicks on say Group B, he/she will of course be directed to groupB.php, where he/she will be able to view the data related to the Group B community (Questions, Discussions posted by the users etc. ). Of course, one will also be able to do the same for the Group A community.

Now my question is: How can I do this efficiently in MySQL? As of now, say if the user wants to create a discussion/question in Group B, which is located on groupB.php, he is directed to a form(discussion.php). Tiny code below:

<form action='savedisc.php' method='post'>

The above form will be available in both communities, i.e. groupA.php and groupB.php. Therefore, of course, savedisc.php will save the content of the form to a database. Meaning, discussion.php and savedisc.php will help in saving both groupA.php and groupB.php's contents. Some of the savedisc.php is below:

$sql="INSERT INTO GroupA (Message, Title, Type)
VALUES
('$message','$title','$represents')";

Where GroupA is the Table's name. The Table name will change of course respective to which group the user may be interacting in. So for group B, the above code will look the same but GroupA will be replaced by GroupB. How can I do this in savedisc.php? Will I have to use numerous IF statements for various groups?

Given the above steps, is this even possible? Can discussion.php be located on all community pages? Because then, savedisc.php's job would be to save contents of groups A, B, C, D in their respective tables within the same database, but the respective group's table. Am I even on the right track?

Thank you.

+1  A: 

You will want to spend some time research relational databases; however, here is a quick high-level answer.

You want to have a few tables: users, groups, groupmems, questions, answers.

users (id, username, group*) -- just an idea and not needed necessarily groups (id, name) groupmems (id, groupid, userid) questions (id, Title, Message, Userid, groupid, date) answers (id, questionid, title, message, userid, date)

What you do is make it where these tables relate to each other through the existence of auto-incrementing primary keys. If you choose to have users login then you will need the users and groups tables for greater flexibility. If you choose not to, then when a user wants to post a new question or answer you will use the PHP POST method to send the value of the group id to the form, which will be bound to a hidden text field.

websch01ar
+1  A: 

that's very easy
there should be only one table with group field. so, you have to have only one set - one form, one script, one table. just pass group identifier using query string and hidden input field

Col. Shrapnel
@ - Col. Shrapnel - I will try that. Will that concept work even if the users are required to log in to the website?
Newbie_25
@newbie nothing to interfere. why? want to log users in - do it. what's wrong with it? Add users table and all these tables which other answer about. Actually it is not "concept" but very basic database rule called "normalization". Having 2 similar tables, which only difference is in the names is one of deadliest sins in database world. When you see such a tables - you always know you're going wrong way. That's it.
Col. Shrapnel