Jump to content

Archived

This topic is now archived and is closed to further replies.

tsiomicron

PHP + MYSQL + LEFT JOIN(?) Help

Recommended Posts

Hello good people!

Ok, iv'e been thinking about how to do this for month's and i am starting to give up hope.

This is extremely difficult for me, and i still havent found a solution, so i will ask if there is anyone out there who can help me write this code. All i've gotten earlyer is some SQL tips to look at left joins but that hasent gotten me far.

Here is what im trying to do.

I got two tables:

[b]table1_members
table2_signups[/b]

the columns i want to use from [b]table1[/b] is [b]names[/b] and [b]ranks[/b]

and the columns from [b]table2[/b] is [b]names[/b] and [b]ranks[/b]

table 1 is constant.... the users and other details are in there, however table two is not.

When someone adds themselves at [b]table2[/b], their names are inserted, but what i want to do is to search [b]table1[/b] for the username submitted in [b]table2[/b], and if it finds a match i want the user rank in [b]table1[/b] to be inserted into table2.

[b]table2[/b] will be emptyed all the time, so the query will need to be done every time a new person is adding themselves.

The ranks themselves dont even need to be inserted to [b]table2.ranks[/b] for all i care, i just need em echoed on the screen really.

So basically: compare [b]table2[/b] to [b]table1[/b] --> if match get rank from [b]table1[/b] --> echo on screen [b]or[/b] insert into [b]table2.ranks[/b]

Is this at all possible?

Can anyone help me write this code, because i cant wrap my head around it.

[b]-OmiCron-[/b]

Share this post


Link to post
Share on other sites
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] names, rank [color=green]FROM[/color] [color=orange]table1,[/color] table2 [color=green]WHERE[/color] table1.names [color=orange]=[/color] table2.names [!--sql2--][/div][!--sql3--]

Unless I'm still spaced from yesterday, this will do what you want.

Which is, selecting all the ranks and usernames for users in table2, right?

Share this post


Link to post
Share on other sites
Hehe, i think you are a litte spaced :P hehe nah.. just kidding.

what i want to do is check table1 for the name that is inserted to table2... if a match is found, i want the rank that is in table1 to be inserted into table2.rank

this is the link to the project i am working on. [a href=\"http://duskronin.mine.nu/gate.html?name=raidbooking&file=raid&id=89\" target=\"_blank\"]Link[/a]
If it's of any help :P

[b]-OmiCron-[/b]

Share this post


Link to post
Share on other sites
I would work on security first.

I'll PM you.

Share this post


Link to post
Share on other sites
[!--quoteo(post=373503:date=May 13 2006, 08:44 AM:name=tsiomicron)--][div class=\'quotetop\']QUOTE(tsiomicron @ May 13 2006, 08:44 AM) [snapback]373503[/snapback][/div][div class=\'quotemain\'][!--quotec--]
what i want to do is check table1 for the name that is inserted to table2... if a match is found, i want the rank that is in table1 to be inserted into table2.rank
[/quote]

Whaaat? [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /] [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]The ranks themselves dont even need to be inserted to table2.ranks for all i care, i just need em echoed on the screen really.[/quote]

And that is what above does, for all users who's name are in table2. No need for double data, I would also suggest replacing the names in table1 by the id's of table1. When your tables get big, you won't have unnessary megabytes in your database.. [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /]

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] names, rank [color=green]FROM[/color] [color=orange]table1,[/color] table2 [color=green]WHERE[/color] table1.id [color=orange]=[/color] table2.table1_id [!--sql2--][/div][!--sql3--]

To insert a row into table2 when you've only got the username:

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']INSERT[/span] [color=green]INTO[/color] [color=orange]table2[/color] (table1_id, other_col) VALUES(([span style=\'color:blue;font-weight:bold\']SELECT[/span] id [color=green]FROM[/color] [color=orange]table1[/color] [color=green]WHERE[/color] name [color=orange]=[/color][color=red]"$username"[/color]),[color=red]"$someValue"[/color]) [!--sql2--][/div][!--sql3--]

It seems you've published your site prematurely though, according to rab...

Share this post


Link to post
Share on other sites
Nah, wasn't his site. It was PHP-nuke's problem.

Share this post


Link to post
Share on other sites
hehe sorry guys. I have a hard time explaining what im thinking when it comes to php and mysql.

But i worked it out with no joins.

i just did like this:

[code]
$query = mysql_query("SELECT * FROM ".$mysql['pre']."signups WHERE raid_id = ".$id) or die(mysql_error());
$i = 0;
$name_exist = false;
while ($row = mysql_fetch_assoc($query)) {
if( $row['name'] == $_POST["name1"]) $name_exist = true;;
}
if( !$name_exist and $_POST["name1"] != "" ) {

$query = mysql_query("SELECT name,guild_title FROM roster_members WHERE name = '$name1'") or die(mysql_error());
$guild_title = mysql_fetch_assoc($query);

if ( count(mysql_fetch_assoc($query)) == 0) {
$e = "Insert into ".$mysql['pre']."signups (name,class,guild,raid_id,text) VALUES ('".htmlspecialchars($_POST["name1"])."','".htmlspecialchars($_POST["class"])."','".htmlspecialchars($_POST["guild"])."',$id,'".htmlspecialchars($_POST["comment"])."')";

}else{
$e = "Insert into raidsign_signups (name,class,guild,raid_id,text,guild_title) VALUES ('".htmlspecialchars($_POST["name1"])."','".htmlspecialchars($_POST["class"])."','".htmlspecialchars($_POST["guild"])."',$id,'".
htmlspecialchars($_POST["comment"])."','".htmlspecialchars($guild_title["guild_title"])."')";
}[/code]

Thx for your help :) much appreciated

[b]-OmiCron-[/b]

Share this post


Link to post
Share on other sites
Glad that it worked, but it's hardly solid code, plus your database design is flawed, too much superplus data. You just can't make a proper database application without using joins, trust me on that.

Share this post


Link to post
Share on other sites

×

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.