简体   繁体   English

从一个表中查找并提取数据并将其插入到 php 和 mysql 中的另一个表中

[英]find and extract data from one table and insert it into another in php and mysql

I am creating a sales system and I want to know the cost of a product through its code / id.我正在创建一个销售系统,我想通过其代码/ID 了解产品的成本。 The part of uploading all the data of a product is fine, but I want to know the cost and that cost to introduce it to another table.上传产品所有数据的部分很好,但我想知道成本以及将其引入另一个表的成本。

THIS IS IN THE PRODUCT TABLE:这是在产品表中:

CODE         NAME         COST   PRICE
44730        doritos        1    2
447390       ice            4    6

As an example I want to know what is the cost of the product with the code 44730, for that I have to use the following:例如,我想知道代码为 44730 的产品的成本是多少,为此我必须使用以下内容:

$db = mysqli_connect("localhost", "root", "", "db_midatabase");

$cost = mysqli_query($db, "
SELECT cost FROM product WHERE code=44730
");

Now that same data that in theory should be 1 I want to introduce it in another table that should do this:现在,理论上应该是 1 的相同数据我想将它引入另一个应该执行此操作的表中:

$sql = "
INSERT INTO myothertable (cost) VALUES ('$cost')";
mysqli_query($db, $sql);

and there is my mistake and it is that simply if you add something to the database but the data is 0, someone who helps me solve this problem please I would appreciate it我的错误是,如果您向数据库添加一些内容但数据为 0,请帮助我解决此问题的人,我将不胜感激

You could use a single INSERT/SELECT query您可以使用单个 INSERT/SELECT 查询

INSERT INTO myothertable (cost) 
SELECT cost FROM product 
WHERE code=44730

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

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