gpt4 book ai didi

php - foreach 行插入 foreach

转载 作者:行者123 更新时间:2023-11-29 12:40:19 29 4
gpt4 key购买 nike

我希望有人能在我发疯之前帮助我。

我有 3 张 table :

Table A
SELECT
companypk,
companyname,
logo,
msscope
FROM global_companies

表B

SELECT docpk, 
templatename,
variation,
doctype,
modifydate,
attachment,
`scope`
FROM templates_documents

表C

SELECT documentpk, 
companyfk,
docref,
doctitle,
doctype,
docowner,
revision,
issuedate,
reasonforchange,
status,
attachment
FROM documents_doucments

所有表格都不相关。然而,我想要实现的是,当我在表 A 上添加新客户端时,我的代码会查找它们在表 A 中的范围,然后将其与表 B 匹配,然后将相关文档插入表 C 中。到目前为止这是我的代码:

//Add Template Documentation
$sql = "SELECT doctype, templatename FROM templates_documents WHERE scope = '".$values["msscope"]."'";
$rs = CustomQuery($sql);
$results = $rs;
while ($row = $results->fetch_assoc())
{
"INSERT INTO documents_doucments (companyfk, doctype, doctitle) values('".$values["companypk"]."',$row[doctype], $row[templatename])";
}

我知道查询部分可以工作,因为我尝试回显结果,并且显示了正确的列信息,但是它没有将任何内容插入到表 C 中。到目前为止,我已经尝试将 while 更改为 foreach。我也是从 client_after_add.php 页面执行此操作,这就是为什么 companypk 是一个 session 字段并与我拥有的其他示例一起使用。

欢迎任何想法。

最佳答案

首先,您似乎没有将 Insert 语句发送到数据库中。仅在循环内写入字符串不会对数据库产生任何影响。

其次,您使用 PHP 来执行应在数据库中执行的功能。您可以在单个语句中执行 SELECT 和 INSERT,例如:

INSERT INTO documents_doucments (companyfk, doctype, doctitle) SELECT '".$values["companypk"]."', doctype, templatename FROM templates_documents WHERE scope = '".$values["msscope"]."'

关于php - foreach 行插入 foreach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26217521/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com