2016-09-16 46 views
0

我创建了两个表,这些主要领域:MySQL的加入查询从PHP脚本

  1. registered_users(registration_id,名字,姓氏,LOGIN_ID,密码)
  2. users_profile(PROFILE_ID,registration_id,FIRST_NAME,姓氏,关系类型,地址,电话)

我创建了一个login.php文件,在registered_users上运行查询来检查loginid和password以及成功的登录调用welcome.php文件。

我已经能够在registered_users表上执行查询(检查登录),但是我需要一些帮助来编写一个查询,这将帮助我从users_profile表中检索与登录用户的registration_id相匹配的那些配置文件。

//login.php

<?php 

if (isset($_POST["loginid"])) 
{ 
    $logid=strtolower($_POST["loginid"]); 
    $passwd=$_POST["password"]; 

    require_once 'connection.php'; 

    $q="select email,password,registeration_id from registered_users where email='$logid'"; 

    $result=mysqli_query($conn,$q); 

     $row = mysqli_fetch_array($result); 

    if ($row["password"] == $passwd && $row["email"]==$logid) 

    { 
      session_start(); 

      $_SESSION["login"] = $logid; 

      // I need to nest a query here to select ONLY those profiles that belong to logged-in user 

      $regid=$_row["registeration_id"]; 



$q2="SELECT registered_users.Registeration_id,profile.Relation_type,profile.first_name,profile.last_name from registered_users,profile WHERE registered_users.Registeration_id=$regid"; 



      $result2=mysqli_query($conn,$q2); 

      $row2 = mysqli_fetch_array($result2); 

      $_SESSION["fnm"]= $row2["first_name"]; 
      $_SESSION["lnm"]= $row2["last_name"]; 
      $_SESSION["rtyp"]= $row2["relation_type"]; 

      header("Location: Welcome.php"); 
    } 

    else 

    { 
      echo "Wrong Username or Password!"; 
    } 


?> 

的代码包含SQL连接查询,需要加以固定。

回答

0

你并不需要一个连接在这里...改变这一行

$q2="SELECT registered_users.Registeration_id,profile.Relation_type,profile.first_name,profile.last_name from registered_users,profile WHERE registered_users.Registeration_id=$regid"; 

这样:

$q2="SELECT profile.Relation_type, profile.first_name, profile.last_name 
    FROM profile 
    WHERE profile.Registeration_id = $regid"; 

这里,Registeration_id是一个foreign key,将检索你需要的数据。

+0

事实上,它在两张桌子上都没有使用连接。我宁愿用这个查询来获取结果: $ q2 =“SELECT registeration_id,relation_type,first_name,last_name FROM profile WHERE registeration_id ='$ regid'”; – Kam