PHP分配主键和外键

时间:2015-11-03 22:09:31

标签: php mysql crud

我尝试将产品分配到类别,我想到的唯一解决方案是使用外键(类Product中的类别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> -->

1 个答案:

答案 0 :(得分:0)

基本上,主键和外键是数据库概念。您必须将产品表category_id设为索引,并将外键设置为类别表id