Need some help with SQL

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Need some help with SQL

    I'm trying to gather associative info from two tables that are held in a third table. Here is my table layout...

    table:contact ----- rows: contact_id, firstname

    table:groups ------rows: group_id, group_name

    table:groupmember ------ rows: contact_id, group_id


    The "groupmember" table contains associative id's relative to group membership.


    Here is the code I have so far.

    Code:
    <td><b>Groups: </b><form name="Group Select" method="Post" action="prcs.viewgroups.php"><select name="viewgroup">
    	<option value="select" selected>Group Membership</option>';
    	
    $result = mysql_query("SELECT group_id FROM groupmember INNER JOIN groups ON groupmember.group_id groups.group_id WHERE groupmember.contact_id = '$contactid'");
    
    while($row = mysql_fetch_array($result))
      {
      echo "<option value=$row[group_id]";
      echo ">$row[group_name]</a></option>";
      }
    
    echo '</select>
    	        &nbsp;<input type="submit" name="Submit" value="Submit" />
    	</td>
    Any idea what I'm doing wrong?

    #2
    Originally posted by Budove View Post
    Code:
    <td><b>Groups: </b><form name="Group Select" method="Post" action="prcs.viewgroups.php"><select name="viewgroup">
    	<option value="select" selected>Group Membership</option>';
    	
    $result = mysql_query("SELECT group_id FROM groupmember INNER JOIN groups ON groupmember.group_id groups.group_id WHERE groupmember.contact_id = '$contactid'");
    
    while($row = mysql_fetch_array($result))
      {
      echo "<option value=$row[group_id]";
      echo ">$row[group_name]</a></option>";
      }
    
    echo '</select>
    	        &nbsp;<input type="submit" name="Submit" value="Submit" />
    	</td>

    Try this,
    Code:
    <td><b>Groups: </b><form name="Group Select" method="Post" action="prcs.viewgroups.php"><select name="viewgroup">
    	<option value="select" selected>Group Membership</option>';
    	
    $result = mysql_query("SELECT groups.group_id, groups.group_name FROM groups INNER JOIN groupmember ON groups.group_id = groupmember.group_id WHERE groupmember.contact_id = '$contactid'");
    
    while($row = mysql_fetch_array($result))
      {
      echo "<option value=$row[0]";
      echo ">$row[1]</a></option>";
      }
    
    echo '</select>
    	        &nbsp;<input type="submit" name="Submit" value="Submit" />
    	</td>

    Comment


      #3
      Thank you for clearing that up. I'm having trouble wrapping my head around INNER JOIN. After studying your code I thought I had it figured out, so I tried to write a query that would return the contact_id and first and last name.. but obviously I haven't quite grasped this concept yet. Here is what I tried to do...

      Code:
      <td><b>Groups: </b><form name="Group Select" method="Post" action="prcs.viewgroups.php"><select name="viewgroup">
      	<option value="select" selected>Group Membership</option>';
      	
      $result = mysql_query("SELECT contact.contact_id, contact.contact_name FROM contact INNER JOIN groupmember ON contact.contact_id = groupmember.contact_id WHERE groupmember.group_id = '$groupid'");
      
      while($row = mysql_fetch_array($result))
        {
        echo "<option value=$row[contact_id]";
        echo ">$row[first] $row[last]</a></option>";
        }
      
      echo '</select>
      	        &nbsp;<input type="submit" name="Submit" value="Submit" />
      	</td>

      Comment


        #4
        Originally posted by Budove View Post
        Thank you for clearing that up. I'm having trouble wrapping my head around INNER JOIN. After studying your code I thought I had it figured out, so I tried to write a query that would return the contact_id and first and last name.. but obviously I haven't quite grasped this concept yet. Here is what I tried to do...

        Code:
        <td><b>Groups: </b><form name="Group Select" method="Post" action="prcs.viewgroups.php"><select name="viewgroup">
        	<option value="select" selected>Group Membership</option>';
        	
        $result = mysql_query("SELECT contact.contact_id, contact.contact_name FROM contact INNER JOIN groupmember ON contact.contact_id = groupmember.contact_id WHERE groupmember.group_id = '$groupid'");
        
        while($row = mysql_fetch_array($result))
          {
          echo "<option value=$row[contact_id]";
          echo ">$row[first] $row[last]</a></option>";
          }
        
        echo '</select>
        	        &nbsp;<input type="submit" name="Submit" value="Submit" />
        	</td>
        see
        Code:
        SELECT contact.contact_id, contact.contact_name FROM contact
        . It only select contact_id & contact_name. Just select first name & last name.

        Comment


          #5
          GAH! Stupid mistake. I thought I wan't understanding. Now I know I did understand it, just wasn't paying attention.

          Thank you so much for clearing it up for me.

          Comment


            #6
            you are welcome always...

            Comment

            Working...
            X