douyanyan1123 2011-08-10 02:50
浏览 40

PHP MySQL添加了另一行功能

I am starting to learn PHP;

i have two tables in my database:

  1. the Brands List that contains Brand_ID, Brand_Name, and Brand_Description.
  2. the Product Lines List that contains Line_ID, Line_Name.

i want to create a third table named Offered Products that contains Offer_Id, Brand_ID, Line_Name - how can i manage this using Foreign Keys?

After creating the table, i want to create a php page that will let users to populate the third table by

  1. Selecting a brand from a dropdown list
  2. Selecting a Line Name from a drop down List
  3. Beside it has a button "Add Row" - which when click will display another row below the first one in order to do number 1 and 2 again and so on
  4. A save button is also present which when clicked will finally save the record on my database.

how can i exactlydo this? please help

  • 写回答

1条回答 默认 最新

  • dpeqsfx5186 2011-08-22 16:14
    关注

    Creating the table.

    Your third table would only need to store Line_ID and Brand_ID and (for whatever reason) you also want Offer_ID.

    CREATE TABLE `Offered Products` (
        /* define columns */
        Offer_ID  INT NOT NULL AUTO_INCREMENT,
        Brand_ID  INT NOT NULL,
        Line_ID   INT NOT NULL,
    
        /* define primary key */
        PRIMARY KEY (Offer_ID)
    
        /* define foreign keys */
        FOREIGN KEY Brand_ID (Brand_ID)
            REFERENCES `Brands List` (Brand_ID)
        FOREIGN KEY Line_ID (Line_ID)
            REFERENCES `Lines List` (Line_ID)
    );
    

    This assumes that the Lines List table (with a space) and Brand List table have Line_ID and Brand_ID respectively defined as primary keys.

    Creating the form.

    Design the form with HTML and PHP. Have each list populated from the corresponding table. I'm not going to provide code for this; it should be straight forward.

    Inserting into the table.

    INSERT INTO `Offered Products` (Brand_ID, Line_ID) VALUES (###, ###)
    

    The ### represents the ID numbers from the HTML form.

    Joining the tables.

    To obtain information from all of the tables you can join as below.

    SELECT * FROM `Offered Products` as op 
        JOIN `Brand List` as bl ON bl.Brand_ID = op.Brand_ID
        JOIN `Line List` as ll on ll.Line_ID = op.Line_ID
    

    Other Notes.

    To use foreign keys in MySQL you need to be using the InnoDB engine. Using myisam will not allow foreign keys, but you can still join the tables as demonstrated to achieve a similar result.

    Avoid using spaces in your table names if you actually did that.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法