简体   繁体   中英

How can I query two tables using php PDO?

I have two tables pre_application and applicants table. I want to query pre_application table for password and applicants table for application pin and if the two table have the records then applicant should be logged in. Below is my code:

if(isset($_POST['submit']) || $_SERVER['REQUEST_METHOD']=='POST'){
                        
    try{
        $pin=$_POST['pin'];
        $pass=$_POST['password'];
        $stm=$dbh->query("SELECT password FROM pre_application WHERE password='$pass' UNION SELECT app_no FROM applicants WHERE app_no='$pin'");
        foreach($stm as $row);
        $appass=$row['password'];
        $appin=$row['app_no'];
        $name=$row['name'];
        if($stm) {
            header('location:approfile.php?applicant=$appass');
        } else {
            echo "<script> alert('Invalid Application Number or Password')</script>";
            exist();
        }
    } catch(Exception $e){
        echo $e->getMessage();
    }                            
}?>

Someone should please help on the right syntax

the UNION is not the correct solution for your case. The UNION , as it name suggests, will combine the data sets (you can also call it rows or records) of all subquery related together.

For your question, I assume that the pre_application and applicants table must be associated through a FOREIGN KEY (the most simple case, otherwise they can be associated via a bridging table , but I cannot tell what exactly there since I cannot see your table structures), otherwise, there is no precise way to link between a pin - password pair and to make sure every password - pin pairs are unique.

With the assumption that there is a column name app_id in the pre_application table that is the FOREIGN KEY of the id column in the applicants table, you can change your query to use JOIN instead of UNION :

 $stm=$dbh->query("SELECT password, pin 
                   FROM pre_application pre
                   JOIN applicants app ON app.id = pre.app_id 
                   WHERE password='$pass' AND app_no='$pin'");

The JOIN match rows from pre_applications table to related one in the applicants table, check to see if these JOINING rows match the password and pin condition, and return the matching one.

You can do some more research about JOIN, UNION here: https://dev.mysql.com/doc/refman/5.7/en/join.html

Thank you all for your contributions. After all this is how I have solved the problem.

<?php
try{
                            $pin=$_POST['pin'];
                            $pass=$_POST['password'];
                            $stm=$dbh->prepare("SELECT pre_application.invoice_no,pre_application.password,applicants.invoice_no,applicants.app_no FROM pre_application INNER JOIN applicants ON pre_application.invoice_no=applicants.invoice_no WHERE pre_application.password=? AND applicants.app_no=?");
                            $stm->bindParam(1, $pass);
                            $stm->bindParam(2, $pin);
                            $stm->execute();
                            $row_count=$stm->rowCount();
                            if($row_count==1){
                                header('location:approfile.php');
                            }else{
                                echo "<script> alert('Invalid Application Number or Password')</script>";

                            }
                        }
                        catch(Exception $e){
                            echo $e->getMessage();
                        }
                        ?>

The above code has worked but I would welcome a better way of achieving the same thing. thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM