Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get information from other table where id=id

soo i have 2 tables.

train_information
user_train_information

When somebody submits something in a form. it get put in the train_information table and looks like this:

what it looks like after the form is submitted

Now, when people are logged in and select the train from a selector. his happens in the database:

when people select the train

On a other page, i want the users to see a whole list of things they selected over the time. So i run a query: SELECT * FROM user_train_information WHERE user_id=user_id;

This shows me the table user_train_information But is it posible to show the train_information where user_id = user_id ? because i want the user to show the trains he added.

EDIT:

What i have now:

function testingggg() {
        $sql = "SELECT *
FROM train_information
INNER JOIN user_train_information
ON train_information.train_id = user_train_information.train_id
WHERE user_train_information.user_id = user_id";
        $sth = $this->pdo->prepare($sql);
        $sth->bindParam("user_id", $_GET["user_id"], PDO::PARAM_INT);
        $sth->execute();
        return $sth->fetchAll();
    }

And i call i here:

<?php
        $summary = $database->testingggg();
    ?>
    <?php
        foreach ($summary as $result){
        echo $result['train_name']; 
    }
?>

I get the error:

error

like image 754
Mitch Avatar asked Nov 26 '22 06:11

Mitch


1 Answers

This is how i use query in such case and it works. In your case there ambiguity somewhere. Also make sure you have proper value in $_GET["user_id"]. Please check

   function testingggg() {
        $sql = "SELECT ti.train_name, ti.train_id, uti.user_id
                FROM train_information as ti
                JOIN user_train_information as uti
                ON ti.train_id = uti.train_id
                WHERE uti.user_id = :user_id";
        $sth = $this->pdo->prepare($sql);
        $sth->bindParam(":user_id", $_GET["user_id"], PDO::PARAM_INT);
        $sth->execute();
        return $sth->fetchAll();
    }
like image 190
Zohaib Avatar answered Nov 29 '22 05:11

Zohaib