简体   繁体   English

检查mysql插入查询

[英]Checking mysql insertion query

I wrote a query but now I'm having doubts. 我写了一个查询,但现在有疑问。

The first one I have to insert a CD supplied by a particular supplier and produced by a particular producer. 首先,我必须插入由特定供应商提供并由特定生产商生产的CD。

$sql="INSERT INTO cd (supplier_name, 
                      supplier_address, 
                      producer_name, 
                      producer_address, 
                      cd_title, 
                      cd_type, 
                      cd_year) 
             VALUES ('$_POST[supp_name]', 
                     '$_POST[supp_addr]', 
                     '$_POST[prod_name]', 
                     '$_POST[prod_addr]', 
                     '$_POST[cd_title]', 
                     '$_POST[cd_year]' , 
                     '$_POST[cd_type]')";

But then I realized, I have a table called CD with ONLY THREE ATTRIBUTES, title, year and type. 但是后来我意识到,我有一个名为CD的表,其中只有三个属性,标题,年份和类型。 CD(title, year, type). CD(标题,年份,类型)。 However, how I just need to insert the CD information by a particular supplier X and particular producer Y. How do I do that? 但是,我只需要由特定的供应商X和特定的生产商Y插入CD信息,该怎么做?

在此处输入图片说明

Also, my form for data entry looks like this: 另外,我的数据输入表单如下所示:

<form action="cd.php" method="post">
<h4> Enter CD information </h4> 
CD Title: <input type="text" name="cd_title"><br>
CD Year: <input type="text" name="cd_year"><br>
CD Type: <input type="text" name="cd_type"><br>

<h4>Enter supplier information</h4>
Supplier Name:  <input type="text" name="supp_name"><br>
Supplier Address:<input type="text" name="supp_addr"><br>


<h4> Enter producer information </h4> 
Producer Name:<input type="text" name="prod_name"><br>
roducer Address:<input type="text" name="prod_addr"><br>

<input type="submit" name="submit" value="Submit">

So, I'm wondering if a user enters the above data, where does it get stored? 因此,我想知道用户是否输入上述数据,该数据存储在哪里? Under what table? 在什么桌子下? (That is, where does the supplier and producer info get stored? (即,供应商和生产商信息存储在哪里?

I'm not sure exactly how you mean, but I'll give it a try: 我不确定您的意思到底是什么,但我将尝试一下:

Try using a select with joins to Supplier and Producer table: 尝试对带有“供应商”和“生产者”表的联接使用选择:

SELECT * FROM CD
JOIN Supplier S ON CD.Supplied = S.Supplied
JOIN Producer P ON CD.Produced = P.Produced
WHERE S.name = X AND P.name = Y

If above query returns true, then do the insert. 如果以上查询返回true,则执行插入操作。

UPDATE: Now when looking at your html-form I think I understand what you're looking for... 更新:现在,当您查看您的html表单时,我想我了解您在寻找什么...

You want a relationsship between CD-table and Producer-table AND a relationsship between CD-table and Supplier-table. 您需要CD表和生产者表之间的关系以及CD表和供应商表之间的关系。

You do this relation by adding and supplier_id-column in your CD-table and a producer_id**-column in your CD-table 您可以通过在CD表中添加和vendor_id-column以及在CD表中添加producer_id **-column来实现这种关系

AND

an autoincremental id in both Supplier-table and in the Producer-table. 供应商表和生产者表中的自动增量ID。

The last step with the actual db is to to create constraints between supplier_id and id-column of supplier-table and producer_id and id-column of producer-table. 实际数据库的最后一步是在Supplier_id和Supplier-Table的ID列与producer_id和Producer-Table的ID列之间创建约束。

When you've done that, you might do something like this: 完成此操作后,您可以执行以下操作:

//When form is submitted...


//First query  (Insert this only if it doesn't already exist in Supplier-table - IGNORE keyword takes care of this)
$sql1="INSERT IGNORE INTO Supplier (supplier_name, 
                      supplier_address) 
             VALUES ('$_POST[supp_name]', 
                     '$_POST[supp_addr]', 
                     )";

//Execute query $sql1
//Get last inserted id from query $sql1 and store it in $idLastInsertedSupplier


//Second query  (Insert this only if it doesn't already exist in Producer-table - IGNORE keyword takes care of this) 
$sql2="INSERT IGNORE INTO Producer (producer_name, 
                      producer_address) 
             VALUES ('$_POST[prod_name]', 
                     '$_POST[prod_addr]')";

//Execute query $sql2
//Get last inserted id from query $sql2 and store it in $idLastInsertedProducer

//Insert CD-table query: 
    $sql="INSERT INTO CD (
                          cd_title, 
                          cd_type, 
                          cd_year,
                          supplier_id,
                          producer_id
                          ) 
   VALUES ('$_POST[cd_title]', 
                         '$_POST[cd_year]' , 
                         '$_POST[cd_type]',
$idLastInsertedSupplier,$idLastInsertedProducer)";

Of course you should use prepared statements (with placeholders) with PDO or Mysqli. 当然,您应该在PDO或Mysqli中使用准备好的语句(带有占位符)。 I have not taken security of SQL into account (not using placeholder, not sanitizing data etc). 我没有考虑到SQL的安全性(不使用占位符,不清理数据等)。

Why id's? 为什么是ID? You could insert into all values into all three tables without having references (ids) between the tables, but then there would be no point of having a relational database. 您可以将所有值插入所有三个表中,而无需在表之间使用引用(id),但是那样就没有关系数据库的意义了。 It would also be much harder to do all SQL-operations afterward. 之后也很难进行所有SQL操作。 Let's say you would want a list of all cd's with their producers and suppliers. 假设您想要包含所有CD的列表以及它们的生产商和供应商。

With relational storage (with their id's) you could execute an sql-statement like this: 使用关系存储(及其ID),您可以执行如下的sql语句:

SELECT * FROM CD
JOIN Supplier S ON CD.id = S.id
JOIN Producer P ON CD.id = P.id

and just fetch the rows. 然后获取行。

There is no good way of doing the same thing without having these references. 没有这些参考文献,就没有做好相同事情的好方法。 (It's impossible for the db to know which row in producer-table that belongs to which row in the CD-table without having any reference). (如果没有任何引用,db就不可能知道生产者表中的哪一行属于CD表中的哪一行)。 You COULD of course add name of producers and suppliers in the CD-table also, but then there would be duplicates and for the most of the time you don't want that (in some extreme cases for speed it would be the only option to have these duplicates though). 当然,您也可以在CD表中添加生产商和供应商的名称,但随后会出现重复,并且在大多数情况下您不希望这样做(在某些极端情况下,为了提高速度,它是唯一的选择尽管有这些重复项)。

I hope I made it clearer now. 我希望我现在把它弄清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM