Jump to content

[SOLVED] displaying more than one group


contra10

Recommended Posts

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...";
}
   
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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';

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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());
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.