Reference child table with master table

In my database, I have a task table, which you can say is the main table in my database. I have a column called category, and this column points to a table called a category that contains different categories.

Taking up the concept of foreign keys, I turned the categories of columns into a foreign key that looks at the category table.

External key

In my category table, I make sure that it points to an identifier. FK implementation

When I launch my web page, it prints the value 1 in the category column, when theoretically it should not print “Driving”? enter image description here

function getJobDetails($job,$cat){
//this connects to the database
include "connectToDatabse.php";

//show me the results from job, where category is like cat vice versa
$results = $pdo->query("SELECT * FROM job WHERE category LIKE '$cat%' OR title LIKE '$job'");
$str = "<table>";                       //prints out table
$str .= "<td>" ."Title" . "</td>";      //first row
$str .= "<td>" ."Reference" . "</td>";  //N row...
$str .= "<td>" ."Salary(£)" . "</td>";
$str .= "<td>" ."Description" . "</td>";
$str .= "<td>" ."Category" . "</td>";
foreach ($results as $row) {
    $ref = $row['reference'];
    $link = "<form method='get' action='apply.php' name='edit'>
         <input type='hidden' name='referenceNumber' value='$ref'>
         <input type='submit' value='$ref'>
    </form>";
    $str .= "<tr>";
    $str .= "<td>" . $row['title'] . "</td>";
    $str .= "<td>" . $row['reference'] . "</td>";
    $str .= "<td>" . $row['salary'] . "</td>";
    $str .= "<td>" . $row['description'] . "</td>";
    $str .= "<td>" . $row['category'] . "</td>";
    $str .= "<td> " .$link . "</td>";
    $str .= "</tr>";
}
$str .= "</table>";
echo $str;
}

The above code is a function that returns data in a job table.

: , , ?

+4
1

, . . , , ", - , , , , X (, ,... ) '. :

SELECT ..., category.title FROM job LEFT JOIN category ON category.id = job.category
+3

Source: https://habr.com/ru/post/1624657/


All Articles