我有两张桌子。非政府组织和志愿者。志愿者正在选择感兴趣的非政府组织。 NGO登录后,我只想显示志愿者表中那些对当前已登录NGO感兴趣的记录。我的代码如下:

[<?php
            if(!$db){
                die("Connection Failed: ".mysqli_connect_error());
            }
            else{
                echo"Success!<br>";
            }
            $current_user=$_SESSION\['login_user'\];
            echo $current_user;
            $sql1=mysqli_query($db,"select *,Name,ngo.EmailId from volunteer,ngo where ngo.EmailId='".$_SESSION\['login_user'\]."' and volunteer.OName=ngo.Name");
            $row=mysqli_num_rows($sql1);

            echo"<table><tr><th>Volunteer Name</th>
                            <th>Volunteer EmailId</th>
                            <th>BirthDate</th>
                            <th>PhoneNo</th>
                            <th>Gender</th>
                            <th>Address</th>
                            <th>Volunteer Reason</th>
                        </tr>";

            while($row=mysqli_fetch_array($sql1)){
                //if($result->num_rows>0){
                    //while($row=$sql->fetch_assoc()){
                    echo"<tr><td>".$row['VName']."</td>
                            <td>".$row['EmailId']."</td>
                            <td>".$row['Birthdate']."</td>
                            <td>".$row['PhoneNo']."</td>
                            <td>".$row['Gender']."</td>
                            <td>".$row['Address']."</td>
                            <td>".$row['VolReason']."</td>
                        </tr>";
            }
                //}
            echo"</table>";
        ?>


这是它的输出:Click here
事件正在打印当前用户,但未将$ current_user与NGO列名EmailID进行比较。

最佳答案

不确定您的表布局是什么样的,但是出于这个原因,您说有一个NGO表,每个表都有一个ID,然后在志愿者表中有一列代表志愿者选择的NGO(我只是称其为“ chosen_ngo” )。然后您的查询将类似于:

SELECT * FROM volunteers INNER JOIN ngo ON ngo.id=volunteers.chosen_ngo WHERE ngo.email='$email_variable'

09-28 13:07