Jump to content


Photo

PHP + MYSQL + LEFT JOIN(?) Help


  • Please log in to reply
7 replies to this topic

#1 tsiomicron

tsiomicron
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 13 May 2006 - 04:24 AM

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:

table1_members
table2_signups


the columns i want to use from table1 is names and ranks

and the columns from table2 is names and ranks

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

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

table2 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 table2.ranks for all i care, i just need em echoed on the screen really.

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

Is this at all possible?

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

-OmiCron-


#2 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 13 May 2006 - 06:59 AM

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] names, rank FROM table1, table2 WHERE table1.names = 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?

#3 tsiomicron

tsiomicron
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 13 May 2006 - 01:44 PM

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

-OmiCron-


#4 rab

rab
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts

Posted 13 May 2006 - 02:11 PM

I would work on security first.

I'll PM you.

#5 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 14 May 2006 - 03:46 PM

[!--quoteo(post=373503:date=May 13 2006, 08:44 AM:name=tsiomicron)--][div class=\'quotetop\']QUOTE(tsiomicron @ May 13 2006, 08:44 AM) View Post[/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 FROM table1, table2 WHERE table1.id = 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] INTO table2 (table1_id, other_col) VALUES(([span style=\'color:blue;font-weight:bold\']SELECT[/span] id FROM table1 WHERE name ="$username"),"$someValue") [!--sql2--][/div][!--sql3--]

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

#6 rab

rab
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts

Posted 14 May 2006 - 04:37 PM

Nah, wasn't his site. It was PHP-nuke's problem.

#7 tsiomicron

tsiomicron
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 15 May 2006 - 02:47 AM

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:

$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"])."')";
}

Thx for your help :) much appreciated

-OmiCron-

#8 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 15 May 2006 - 05:51 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users