contra10 Posted December 24, 2008 Share Posted December 24, 2008 I have a table in my users that shows thee group that a user is in, however if a user joins another group, it updates is there a way to input that group into a table without deleting the previous group and echo the groups the users joined <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); if (isset($_POST['submit'])){ if(is_numeric($_POST['grp'])){ $id = $_POST['grp']; $query= "SELECT * FROM groups WHERE id = $id"; $result = mysql_query($query) or die(mysql_error());; $group = mysql_fetch_assoc($result); $groupname = $group['name']; mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); $insert = "UPDATE users SET mygroups = '$groupname' WHERE username = '$username'"; $add_group = mysql_query($insert) or die(mysql_error()); } else { echo $_POST['grp'] . ": Is not numeric..."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138271-solved-displaying-more-than-one-group/ Share on other sites More sharing options...
trq Posted December 24, 2008 Share Posted December 24, 2008 A users groups should be stored within a seperate table. Then all you would need do is simply add a new record for each group a user belongs too. Quote Link to comment https://forums.phpfreaks.com/topic/138271-solved-displaying-more-than-one-group/#findComment-722944 Share on other sites More sharing options...
contra10 Posted December 24, 2008 Author Share Posted December 24, 2008 ye my users is in a seperate table and my groups is in a seperate table I transferred the name of the group into "mygroups" in the users table and echoed mygroups...is there a way to create a list in the mygroups row or is there a way that if a user enters a group it adds a new row...kinda confused about that Quote Link to comment https://forums.phpfreaks.com/topic/138271-solved-displaying-more-than-one-group/#findComment-722946 Share on other sites More sharing options...
trq Posted December 24, 2008 Share Posted December 24, 2008 You need three tables. This is a simple example. CREATE TABLE users ( uid INT, uname VARCHAR(80) ); CREATE TABLE groups ( gid INT, gname VARCHAR(80) ); CREATE TABLE users_groups ( ugid INT, uid INT, gid INT ); Now add three types of groups. INSERT INTO groups (gid,gname) VALUES (1, 'admin'); INSERT INTO groups (gid,gname) VALUES (2, 'mod'); INSERT INTO groups (gid,gname) VALUES (3, 'user'); Now add a user. INSERT INTO users (uid,uname) VALUES (1, 'thorpe'); Now, to make thorpe belong to the admin and user groups. INSERT INTO users_groups (ugid, uid, gid) VALUES (1, 1, 1); INSERT INTO users_groups (ugid, uid, gid) VALUES (1, 1, 3); Do you see the relationship? Quote Link to comment https://forums.phpfreaks.com/topic/138271-solved-displaying-more-than-one-group/#findComment-722949 Share on other sites More sharing options...
contra10 Posted December 24, 2008 Author Share Posted December 24, 2008 ok i see the relationship....so since i have a group table and a user table should i create just create a user_group table Quote Link to comment https://forums.phpfreaks.com/topic/138271-solved-displaying-more-than-one-group/#findComment-722951 Share on other sites More sharing options...
trq Posted December 24, 2008 Share Posted December 24, 2008 Indeed. Quote Link to comment https://forums.phpfreaks.com/topic/138271-solved-displaying-more-than-one-group/#findComment-722952 Share on other sites More sharing options...
contra10 Posted December 24, 2008 Author Share Posted December 24, 2008 ok so now if a user joins another group they will still be apart of a previous group they entered? so i could echo all the groups the user is apart of Quote Link to comment https://forums.phpfreaks.com/topic/138271-solved-displaying-more-than-one-group/#findComment-722953 Share on other sites More sharing options...
trq Posted December 24, 2008 Share Posted December 24, 2008 ok so now if a user joins another group they will still be apart of a previous group they entered? Yes, you simply add another row to the users_groups table. so i could echo all the groups the user is apart of Yes, for instance this query would get all the grups that 'thorpe' belongs too. SELECT groups.gname FROM users JOIN users_groups ON users_groups.uid = users.uid JOIN groups ON groups.gid = users_groups.gid WHERE users.uname = 'thorpe'; Quote Link to comment https://forums.phpfreaks.com/topic/138271-solved-displaying-more-than-one-group/#findComment-722956 Share on other sites More sharing options...
contra10 Posted December 24, 2008 Author Share Posted December 24, 2008 it makes sense....but i'm so lost SORRY heres the code <?php // Connects to your Database mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); //This code runs if the form has been submitted if (isset($_POST['submit'])) { //This makes sure they did not leave any fields blank if (!$_POST['name'] | !$_POST['group'] | !$_POST['description']) { die('You did not complete all of the required fields'); } // checks if the username is in use if (!get_magic_quotes_gpc()) { $_POST['name'] = ($_POST['name']); } $groupcheck = $_POST['name']; $check = mysql_query("SELECT name FROM groups WHERE name = '$groupcheck'") or die(mysql_error()); $check2 = mysql_num_rows($check); //if the name exists it gives an error if ($check2 != 0) { die('Sorry, the group '.$_POST['name'].' is already in use.'); } // this makes sure both passwords entered match if ($_POST['name'] == $_POST['group']) { die('Input a different name please. '); } $name = mysql_real_escape_string($_POST['name']); $group = mysql_real_escape_string($_POST['group']); $description = mysql_real_escape_string($_POST['description']); $city = mysql_real_escape_string($_POST['city']); $email = mysql_real_escape_string($_POST['email']); // now we insert it into the database $insert = "INSERT INTO groups (name,`group`,description,city, email) VALUES ('$name', '$group', '$description', '$city', '$email')"; $add_group = mysql_query($insert) or die(mysql_error()); $insert2="INSERT INTO groups (id,creator) VALUES (1, 'admin')"; $insert3="INSERT INTO groups (id,creator) VALUES (2, 'mod')"; $insert4="INSERT INTO groups (id,creator) VALUES (3, 'user')"; $add_group2 = mysql_query($insert2) or die(mysql_error()); $add_group3 = mysql_query($insert3) or die(mysql_error()); $add_group4 = mysql_query($insert4) or die(mysql_error()); $insert5="INSERT INTO userg (uid,uname) VALUES (1, '$username')"; $add_group5 = mysql_query($insert5) or die(mysql_error()); $insert6="INSERT INTO users_groups (ugid, uid, gid) VALUES (1, 1, 1)"; $add_group6 = mysql_query($insert6) or die(mysql_error()); ?> i have 4 tables groups group_users userg users(inputs are from registration) Quote Link to comment https://forums.phpfreaks.com/topic/138271-solved-displaying-more-than-one-group/#findComment-722957 Share on other sites More sharing options...
trq Posted December 24, 2008 Share Posted December 24, 2008 Your groups table should only contain the fields id (usually auto incrementing) and a field to store the name of the group. All information related to each individual user should go within the users table. Or, if you wanted to get real fancy (and normalize some more) you could split users information off into another table. Quote Link to comment https://forums.phpfreaks.com/topic/138271-solved-displaying-more-than-one-group/#findComment-722960 Share on other sites More sharing options...
contra10 Posted December 24, 2008 Author Share Posted December 24, 2008 k i entered it, and the first time all the variables entered like u said it would umm thing is when i let the user i logged in with try to create another group it says Duplicate entry '1' for key 1 <?php // Connects to your Database mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); //This code runs if the form has been submitted if (isset($_POST['submit'])) { //This makes sure they did not leave any fields blank if (!$_POST['name'] | !$_POST['group'] | !$_POST['description']) { die('You did not complete all of the required fields'); } // checks if the username is in use if (!get_magic_quotes_gpc()) { $_POST['name'] = ($_POST['name']); } $groupcheck = $_POST['name']; $check = mysql_query("SELECT name FROM groups WHERE name = '$groupcheck'") or die(mysql_error()); $check2 = mysql_num_rows($check); //if the name exists it gives an error if ($check2 != 0) { die('Sorry, the group '.$_POST['name'].' is already in use.'); } // this makes sure both passwords entered match if ($_POST['name'] == $_POST['group']) { die('Input a different name please. '); } $name = mysql_real_escape_string($_POST['name']); $group = mysql_real_escape_string($_POST['group']); $description = mysql_real_escape_string($_POST['description']); $city = mysql_real_escape_string($_POST['city']); $email = mysql_real_escape_string($_POST['email']); // now we insert it into the database $insert = "INSERT INTO groups (name,`group`,description,city, email) VALUES ('$name', '$group', '$description', '$city', '$email')"; $add_group = mysql_query($insert) or die(mysql_error()); $insert2="INSERT INTO groupsa (id,name) VALUES (1, 'admin')"; $insert3="INSERT INTO groupsa (id,name) VALUES (2, 'mod')"; $insert4="INSERT INTO groupsa (id,name) VALUES (3, 'user')"; $add_group2 = mysql_query($insert2) or die(mysql_error()); $add_group3 = mysql_query($insert3) or die(mysql_error()); $add_group4 = mysql_query($insert4) or die(mysql_error()); $insert5="INSERT INTO userg (uid,uname) VALUES (1, '$username')"; $add_group5 = mysql_query($insert5) or die(mysql_error()); $insert6="INSERT INTO groups_users (ugid, uid, gid) VALUES (1, 1, 1)"; $add_group6 = mysql_query($insert6) or die(mysql_error()); ?> sry if i keep messing up Quote Link to comment https://forums.phpfreaks.com/topic/138271-solved-displaying-more-than-one-group/#findComment-722967 Share on other sites More sharing options...
trq Posted December 24, 2008 Share Posted December 24, 2008 Because you need to use auto incrementing id's. I used hard coded keys simply to make my example more descriptive. Quote Link to comment https://forums.phpfreaks.com/topic/138271-solved-displaying-more-than-one-group/#findComment-722971 Share on other sites More sharing options...
trq Posted December 24, 2008 Share Posted December 24, 2008 Also, you don't need to add new group types every time. If you want the group types admin, mod and user you'll only need to add them once. Quote Link to comment https://forums.phpfreaks.com/topic/138271-solved-displaying-more-than-one-group/#findComment-722972 Share on other sites More sharing options...
contra10 Posted December 24, 2008 Author Share Posted December 24, 2008 jus want to see if im kinda on the right track should i get the ids in a query and transfer them to the other tables <?php // Connects to your Database mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); //This code runs if the form has been submitted if (isset($_POST['submit'])) { //This makes sure they did not leave any fields blank if (!$_POST['name'] | !$_POST['group'] | !$_POST['description']) { die('You did not complete all of the required fields'); } // checks if the username is in use if (!get_magic_quotes_gpc()) { $_POST['name'] = ($_POST['name']); } $groupcheck = $_POST['name']; $check = mysql_query("SELECT name FROM groups WHERE name = '$groupcheck'") or die(mysql_error()); $check2 = mysql_num_rows($check); //if the name exists it gives an error if ($check2 != 0) { die('Sorry, the group '.$_POST['name'].' is already in use.'); } // this makes sure both passwords entered match if ($_POST['name'] == $_POST['group']) { die('Input a different name please. '); } $name = mysql_real_escape_string($_POST['name']); $group = mysql_real_escape_string($_POST['group']); $description = mysql_real_escape_string($_POST['description']); $city = mysql_real_escape_string($_POST['city']); $email = mysql_real_escape_string($_POST['email']); // now we insert it into the database $insert = "INSERT INTO groups (name,`group`,description,city, email) VALUES ('$name', '$group', '$description', '$city', '$email')"; $add_group = mysql_query($insert) or die(mysql_error()); $insert2="INSERT INTO groupsa (id,name) VALUES (1, 'admin')"; $insert3="INSERT INTO groupsa (id,name) VALUES (2, 'mod')"; $insert4="INSERT INTO groupsa (id,name) VALUES (3, 'user')"; $add_group2 = mysql_query($insert2) or die(mysql_error()); $add_group3 = mysql_query($insert3) or die(mysql_error()); $add_group4 = mysql_query($insert4) or die(mysql_error()); $query = "SELECT id FROM users WHERE username= '$username'"; $result = mysql_query($query); $rowid = mysql_fetch_assoc($result) $id= "{$rowid['id']}"; $insert5="INSERT INTO userg (uid,uname) VALUES ('$id', '$username')"; $add_group5 = mysql_query($insert5) or die(mysql_error()); $insert6="INSERT INTO groups_users (ugid, uid, gid) VALUES (1, 1, 1)"; $add_group6 = mysql_query($insert6) or die(mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/138271-solved-displaying-more-than-one-group/#findComment-722981 Share on other sites More sharing options...
contra10 Posted December 24, 2008 Author Share Posted December 24, 2008 kinda lost Quote Link to comment https://forums.phpfreaks.com/topic/138271-solved-displaying-more-than-one-group/#findComment-723164 Share on other sites More sharing options...
contra10 Posted December 24, 2008 Author Share Posted December 24, 2008 values are entering <?php // Connects to your Database mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); //This code runs if the form has been submitted if (isset($_POST['submit'])) { //This makes sure they did not leave any fields blank if (!$_POST['name'] | !$_POST['group'] | !$_POST['description']) { die('You did not complete all of the required fields'); } // checks if the name is in use if (!get_magic_quotes_gpc()) { $_POST['name'] = ($_POST['name']); } $groupcheck = $_POST['name']; $check = mysql_query("SELECT name FROM groups WHERE name = '$groupcheck'") or die(mysql_error()); $check2 = mysql_num_rows($check); //if the name exists it gives an error if ($check2 != 0) { die('Sorry, the group '.$_POST['name'].' is already in use.'); } // this makes sure both name and group entered match if ($_POST['name'] == $_POST['group']) { die('Input a different name please. '); } $name = mysql_real_escape_string($_POST['name']); $group = mysql_real_escape_string($_POST['group']); $description = mysql_real_escape_string($_POST['description']); $city = mysql_real_escape_string($_POST['city']); $email = mysql_real_escape_string($_POST['email']); // now we insert it into the database $insert = "INSERT INTO groups (name,`group`,description,city, email) VALUES ('$name', '$group', '$description', '$city', '$email')"; $add_group = mysql_query($insert) or die(mysql_error()); $insert2="INSERT INTO groupsa (name) VALUES ('admin')"; $insert4="INSERT INTO groupsa (name) VALUES ('user')"; $add_group2 = mysql_query($insert2) or die(mysql_error()); $add_group4 = mysql_query($insert4) or die(mysql_error()); $query = "SELECT id FROM users WHERE username= '$username'"; $result = mysql_query($query); while($rowid = mysql_fetch_assoc($result)) { $id= "{$rowid['id']}"; } $insert5="INSERT INTO userg (uid,uname) VALUES ('$id', '$username')"; $add_group5 = mysql_query($insert5) or die(mysql_error()); $insert6="INSERT INTO groups_users (ugid, uid, gid) VALUES (1, 1, 1)"; $add_group6 = mysql_query($insert6) or die(mysql_error()); ?> i guess this is right? Quote Link to comment https://forums.phpfreaks.com/topic/138271-solved-displaying-more-than-one-group/#findComment-723168 Share on other sites More sharing options...
contra10 Posted December 25, 2008 Author Share Posted December 25, 2008 hmm Quote Link to comment https://forums.phpfreaks.com/topic/138271-solved-displaying-more-than-one-group/#findComment-723428 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.