title: PHP Assigning Primary Key and Foreign Key
body: <p>I'm trying to assign a Product to a Category and the only solution that I thought of is assigning the Primary Key (Category Id in class Category) with the Foreign Key (Category Id in class Product). </p>
  
  <p>Do i have to do something with the SQL code or change the code in the index.php?</p>
  
  <p>The codes are to show readers what I have done!</p>
  
  <p>Class</p>
  
  <pre><code>public class Category {
      public int Id{get;set;}
      public string Name {get;set;}
      }
  public class Product{
      public int Id{get;set;}
      public int CategoryId{get;set;}
      public String Brand {get;set;}
      public String Name {get;set;}
      public decimal Price{get;set;
  }
  </code></pre>
  
  <p>mydatabase sql</p>
  
  <pre><code>CREATE TABLE  IF NOT EXISTS `product` (
  `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  `category_id` INT NOT NULL ,
  `brand` VARCHAR( 100 ) NOT NULL ,
  `name` VARCHAR( 100 ) NOT NULL ,
  `barcode` INT NOT NULL,
  `price` DECIMAL NOT NULL
  ) ENGINE = INNODB;
  
  INSERT INTO `product` (`id`, `category_id`, `brand`, `name`,`barcode`,`price`) VALUES
  (1,1,'Coca Cola','Coke',123456789,0.99)
  
  CREATE TABLE  IF NOT EXISTS `category` (
  `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  `category_name` VARCHAR( 100 ) NOT NULL
  ) ENGINE = INNODB;
  
  INSERT INTO `category` (`id`, `category_name`) VALUES
  (1,'Drinks')
  </code></pre>
  
  <p>index.php </p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;link   href="css/bootstrap.min.css" rel="stylesheet"&gt;
      &lt;script src="js/bootstrap.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;div class="container"&gt;
              &lt;div class="row"&gt;
                  &lt;h3&gt;PHP CRUD Grid&lt;/h3&gt;
              &lt;/div&gt;
              &lt;div class="row"&gt;
                  &lt;p&gt;
                      &lt;a href="create.php" class="btn btn-success"&gt;Create&lt;/a&gt;
                  &lt;/p&gt;
  
                  &lt;table class="table table-striped table-bordered"&gt;
                        &lt;thead&gt;
                          &lt;tr&gt;
                            &lt;th&gt;Id&lt;/th&gt;
                            &lt;th&gt;CategoryId&lt;/th&gt;
                            &lt;th&gt;Brand&lt;/th&gt;
                            &lt;th&gt;Name&lt;/th&gt;
                        &lt;th&gt;Barcode&lt;/th&gt;
                        &lt;th&gt;Price&lt;/th&gt;
                          &lt;/tr&gt;
                        &lt;/thead&gt;
                        &lt;tbody&gt;
                        &lt;?php
                         include_once 'database.php';
                         $pdo = Database::connect();
                         $sql = 'SELECT * FROM product ORDER BY id DESC';
                         foreach ($pdo-&gt;query($sql) as $row) {
                                  echo '&lt;tr&gt;';
                                  echo '&lt;td&gt;'. $row['id'] . '&lt;/td&gt;';
                                  echo '&lt;td&gt;'. $row['category_id'] . '&lt;/td&gt;';
                                  echo '&lt;td&gt;'. $row['brand'] . '&lt;/td&gt;';
                    echo '&lt;td&gt;'. $row['name'] . '&lt;/td&gt;';
                    echo '&lt;td&gt;'. $row['barcode'] . '&lt;/td&gt;';
                    echo '&lt;td&gt;'. $row['price'] . '&lt;/td&gt;';
                                  echo '&lt;td width=250&gt;';
                                  echo '&lt;a class="btn" href="read.php?id='.$row['id'].'"&gt;Read&lt;/a&gt;';
                                  echo '&amp;nbsp;';
                                  echo '&lt;a class="btn btn-success" href="update.php?id='.$row['id'].'"&gt;Update&lt;/a&gt;';
                                  echo '&amp;nbsp;';
                                  echo '&lt;a class="btn btn-danger" href="delete.php?id='.$row['id'].'"&gt;Delete&lt;/a&gt;';
                                  echo '&lt;/td&gt;';
                                  echo '&lt;/tr&gt;';
                         }
  
                        ?&gt;
                        &lt;/tbody&gt;
                  &lt;/table&gt;
  
                &lt;div class="container"&gt;
                &lt;div class="row"&gt;
                &lt;h3&gt;PHP CRUD Grid&lt;/h3&gt;
                &lt;/div&gt;
                &lt;div class="row"&gt;
                &lt;p&gt;
                &lt;a href="createCategory.php" class="btn btn-success"&gt;Create&lt;/a&gt;
                &lt;/p&gt;
                &lt;table class="table table-striped table-bordered"&gt;
                &lt;thead&gt;
                &lt;tr&gt;
                  &lt;th&gt;CategoryId&lt;/th&gt;
                  &lt;th&gt;Category Name&lt;/th&gt;
                &lt;/tr&gt;
              &lt;/thead&gt;
  
              include_once 'database.php';
              &lt;?php
              $sql2 = 'SELECT * FROM category ORDER BY id DESC';
              foreach ($pdo-&gt;query($sql2) as $row) {
                   echo '&lt;tr&gt;';
                   echo '&lt;td&gt;'. $row['id'] . '&lt;/td&gt;';
                   echo '&lt;td&gt;'. $row['category_name'] . '&lt;/td&gt;';
                   echo '&lt;td width=250&gt;';
                   echo '&lt;a class="btn" href="readCategory.php?id='.$row['id'].'"&gt;Read&lt;/a&gt;';
                   echo '&amp;nbsp;';
                   echo '&lt;a class="btn btn-success" href="updateCategory.php?id='.$row['id'].'"&gt;Update&lt;/a&gt;';
                   echo '&amp;nbsp;';
                   echo '&lt;a class="btn btn-danger" href="deleteCategory.php?id='.$row['id'].'"&gt;Delete&lt;/a&gt;';
                   echo '&lt;/td&gt;';
                   echo '&lt;/tr&gt;';
  
               }
               Database::disconnect();
              ?&gt;
                &lt;/tbody&gt;
              &lt;/table&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
  
    &lt;/body&gt;
  &lt;/html&gt;
        &lt;!--              &lt;div class="container"&gt;
                      &lt;div class="row"&gt;
                      &lt;h3&gt;PHP CRUD Grid&lt;/h3&gt;
                      &lt;/div&gt;
                      &lt;div class="row"&gt;
                      &lt;p&gt;
                      &lt;a href="create.php" class="btn btn-success"&gt;Create&lt;/a&gt;
                      &lt;/p&gt;
                              &lt;table class="table table-striped table-bordered"&gt;
                      &lt;thead&gt;
                      &lt;tr&gt;
                        &lt;th&gt;CategoryId&lt;/th&gt;
                        &lt;th&gt;Catengory Name&lt;/th&gt;
                      &lt;/tr&gt;
                    &lt;/thead&gt;
  
                    $sql2 = 'SELECT * FROM category ORDER BY id DESC';
                    foreach ($pdo-&gt;query($sql2) as $row) {
                         echo '&lt;tr&gt;';
                         echo '&lt;td&gt;'. $row['id'] . '&lt;/td&gt;';
                         echo '&lt;td&gt;'. $row['category_name'] . '&lt;/td&gt;';
  
                         echo '&lt;td width=250&gt;';
                         echo '&lt;a class="btn" href="readCategory.php?id='.$row['id'].'"&gt;Read&lt;/a&gt;';
                         echo '&amp;nbsp;';
                         echo '&lt;a class="btn btn-success" href="updateCategory.php?id='.$row['id'].'"&gt;Update&lt;/a&gt;';
                         echo '&amp;nbsp;';
                         echo '&lt;a class="btn btn-danger" href="deleteCategory.php?id='.$row['id'].'"&gt;Delete&lt;/a&gt;';
                         echo '&lt;/td&gt;';
                         echo '&lt;/tr&gt;';
                       &lt;/div&gt;
                     }
                     Database::disconnect();
                    ?&gt;
                      &lt;/tbody&gt;
                    &lt;/table&gt; --&gt;
  </code></pre>