PHP分配主键和外键


PHP Assigning Primary Key and Foreign Key

我正试图将一个产品分配给一个类别,我想到的唯一解决方案是将主键(类别类别中的类别Id)分配给外键(类别产品中的类别Id)。

我必须对SQL代码做点什么还是更改index.php中的代码?

代码是为了向读者展示我的所作所为!

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;
}

mydatabase-sql-

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')

index.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link   href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/bootstrap.min.js"></script>
</head>
<body>
    <div class="container">
            <div class="row">
                <h3>PHP CRUD Grid</h3>
            </div>
            <div class="row">
                <p>
                    <a href="create.php" class="btn btn-success">Create</a>
                </p>
                <table class="table table-striped table-bordered">
                      <thead>
                        <tr>
                          <th>Id</th>
                          <th>CategoryId</th>
                          <th>Brand</th>
                          <th>Name</th>
                      <th>Barcode</th>
                      <th>Price</th>
                        </tr>
                      </thead>
                      <tbody>
                      <?php
                       include_once 'database.php';
                       $pdo = Database::connect();
                       $sql = 'SELECT * FROM product ORDER BY id DESC';
                       foreach ($pdo->query($sql) as $row) {
                                echo '<tr>';
                                echo '<td>'. $row['id'] . '</td>';
                                echo '<td>'. $row['category_id'] . '</td>';
                                echo '<td>'. $row['brand'] . '</td>';
                  echo '<td>'. $row['name'] . '</td>';
                  echo '<td>'. $row['barcode'] . '</td>';
                  echo '<td>'. $row['price'] . '</td>';
                                echo '<td width=250>';
                                echo '<a class="btn" href="read.php?id='.$row['id'].'">Read</a>';
                                echo '&nbsp;';
                                echo '<a class="btn btn-success" href="update.php?id='.$row['id'].'">Update</a>';
                                echo '&nbsp;';
                                echo '<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Delete</a>';
                                echo '</td>';
                                echo '</tr>';
                       }
                      ?>
                      </tbody>
                </table>
              <div class="container">
              <div class="row">
              <h3>PHP CRUD Grid</h3>
              </div>
              <div class="row">
              <p>
              <a href="createCategory.php" class="btn btn-success">Create</a>
              </p>
              <table class="table table-striped table-bordered">
              <thead>
              <tr>
                <th>CategoryId</th>
                <th>Category Name</th>
              </tr>
            </thead>
            include_once 'database.php';
            <?php
            $sql2 = 'SELECT * FROM category ORDER BY id DESC';
            foreach ($pdo->query($sql2) as $row) {
                 echo '<tr>';
                 echo '<td>'. $row['id'] . '</td>';
                 echo '<td>'. $row['category_name'] . '</td>';
                 echo '<td width=250>';
                 echo '<a class="btn" href="readCategory.php?id='.$row['id'].'">Read</a>';
                 echo '&nbsp;';
                 echo '<a class="btn btn-success" href="updateCategory.php?id='.$row['id'].'">Update</a>';
                 echo '&nbsp;';
                 echo '<a class="btn btn-danger" href="deleteCategory.php?id='.$row['id'].'">Delete</a>';
                 echo '</td>';
                 echo '</tr>';
             }
             Database::disconnect();
            ?>
              </tbody>
            </table>
        </div>
    </div>

  </body>
</html>
      <!--              <div class="container">
                    <div class="row">
                    <h3>PHP CRUD Grid</h3>
                    </div>
                    <div class="row">
                    <p>
                    <a href="create.php" class="btn btn-success">Create</a>
                    </p>
                            <table class="table table-striped table-bordered">
                    <thead>
                    <tr>
                      <th>CategoryId</th>
                      <th>Catengory Name</th>
                    </tr>
                  </thead>
                  $sql2 = 'SELECT * FROM category ORDER BY id DESC';
                  foreach ($pdo->query($sql2) as $row) {
                       echo '<tr>';
                       echo '<td>'. $row['id'] . '</td>';
                       echo '<td>'. $row['category_name'] . '</td>';
                       echo '<td width=250>';
                       echo '<a class="btn" href="readCategory.php?id='.$row['id'].'">Read</a>';
                       echo '&nbsp;';
                       echo '<a class="btn btn-success" href="updateCategory.php?id='.$row['id'].'">Update</a>';
                       echo '&nbsp;';
                       echo '<a class="btn btn-danger" href="deleteCategory.php?id='.$row['id'].'">Delete</a>';
                       echo '</td>';
                       echo '</tr>';
                     </div>
                   }
                   Database::disconnect();
                  ?>
                    </tbody>
                  </table> -->

Basicly、主键和外键是数据库的概念。您必须使产品表category_id成为索引,并为类别表