php如何在当前表输入数据,同时另外一张表指定字段插入关联数据?
代码如下:两张表如下,需要再dd表中插入p_id之后再cg表中增加p_id,同时把sorder_qty的数值同步到cg表的cg_qty中去。dd表+-----------...
代码如下:
两张表如下,需要再dd表中插入p_id之后再cg表中增加p_id,同时把sorder_qty的数值同步到cg表的cg_qty中去。
dd表
+-----------+-----------+---------------+------------+-----------+---------------+------+
| Sorder_id | Sorder_ln | Sorder_des | Sorder_qty | Sorder_up | Sorder_amount | P_id |
+-----------+-----------+---------------+------------+-----------+---------------+------+
| 6 | 1 | torque wrench | 6 | 200.00 | 1200.00 | 3 |
| 7 | 1 | ground tester | 1 | 25000.00 | 25000.00 | 4 |
+-----------+-----------+---------------+------------+-----------+---------------+------+
cg表
+-------+-------+--------------+--------+--------+-----------+------+
| cg_id | cg_ln | cg_des | cg_qty | cg_up | cg_amount | P_id |
+-------+-------+--------------+--------+--------+-----------+------+
| 1 | 1 | | 5 | 150.00 | 0.00 | 1 |
| 2 | 1 | | 10 | 150.00 | 150.00 | 1 |
+-------+-------+--------------+--------+--------+-----------+------+
代码如下,field_1应该是cg表的P_id, new_value_field_1的正确写法是什么,我试了很多,只能在cg表中增加一条空白数据而已,应该是new_value_field_1的写法不正确,请各位帮帮忙。。。
/**
* Insert a record on another table
*/
// SQL statement parameters
$insert_table = 'my_table'; // Table name
$insert_fields = array( // Field list, add as many as needed
'field_1' => "'new_value_field_1'",
'field_2' => "'new_value_field_2'",
);
// Insert record
$insert_sql = 'INSERT INTO ' . $insert_table
. ' (' . implode(', ', array_keys($insert_fields)) . ')'
. ' VALUES (' . implode(', ', array_values($insert_fields)) . ')';
sc_exec_sql($insert_sql); 展开
两张表如下,需要再dd表中插入p_id之后再cg表中增加p_id,同时把sorder_qty的数值同步到cg表的cg_qty中去。
dd表
+-----------+-----------+---------------+------------+-----------+---------------+------+
| Sorder_id | Sorder_ln | Sorder_des | Sorder_qty | Sorder_up | Sorder_amount | P_id |
+-----------+-----------+---------------+------------+-----------+---------------+------+
| 6 | 1 | torque wrench | 6 | 200.00 | 1200.00 | 3 |
| 7 | 1 | ground tester | 1 | 25000.00 | 25000.00 | 4 |
+-----------+-----------+---------------+------------+-----------+---------------+------+
cg表
+-------+-------+--------------+--------+--------+-----------+------+
| cg_id | cg_ln | cg_des | cg_qty | cg_up | cg_amount | P_id |
+-------+-------+--------------+--------+--------+-----------+------+
| 1 | 1 | | 5 | 150.00 | 0.00 | 1 |
| 2 | 1 | | 10 | 150.00 | 150.00 | 1 |
+-------+-------+--------------+--------+--------+-----------+------+
代码如下,field_1应该是cg表的P_id, new_value_field_1的正确写法是什么,我试了很多,只能在cg表中增加一条空白数据而已,应该是new_value_field_1的写法不正确,请各位帮帮忙。。。
/**
* Insert a record on another table
*/
// SQL statement parameters
$insert_table = 'my_table'; // Table name
$insert_fields = array( // Field list, add as many as needed
'field_1' => "'new_value_field_1'",
'field_2' => "'new_value_field_2'",
);
// Insert record
$insert_sql = 'INSERT INTO ' . $insert_table
. ' (' . implode(', ', array_keys($insert_fields)) . ')'
. ' VALUES (' . implode(', ', array_values($insert_fields)) . ')';
sc_exec_sql($insert_sql); 展开
展开全部
你的 dd 表数据插入成功后获取刚才用于插入的数据再新组装一个 SQL 添加进cg表,你写的太复杂了。
/**
* dd 表所有字段
*
* Sorder_id
* Sorder_ln
* Sorder_des
* Sorder_qty
* Sorder_up
* Sorder_amount
* P_id
*/
function fields ($data) {
// 组装你的 keys 和 values
$keys = '';
$values = '';
foreach ($data AS $key => $value) {
$keys .= $key . ',';
$values .= '\'' . $value . '\',';
}
return [
'keys' => preg_replace('/,$/', '', $keys),
'values' => preg_replace('/,$/', '', $values),
];
}
// 数据数组(dd 表)
$data = [
'Sorder_id' => 'Sorder_id',
'Sorder_ln' => 'Sorder_ln',
'Sorder_des ' => 'Sorder_des',
'Sorder_qty' => 'Sorder_qty',
'Sorder_up' => 'Sorder_up',
'Sorder_amount' => 'Sorder_amount',
'P_id' => 'P_id',
];
$result = fields($data);
$sql = "INSERT INTO tablename (" . $result['keys'] . ") VALUES (" . $result['values'] . ")";
echo $sql; // 插入
// cg 表的数据数组
$dataCg = [
'cg_id' => 'cg_id',
'cg_ln' => 'cg_ln',
'cg_des' => 'cg_des',
'cg_qty' => $data['Sorder_qty'], // 同步 dd 表中的 sorder_qty
'cg_up' => 'cg_up',
'cg_amount' => 'cg_amount',
'P_id' => $data['P_id'], // 同步 dd 表中的 P_id
];
$result = fields($dataCg);
$sql = "INSERT INTO tablename (" . $result['keys'] . ") VALUES (" . $result['values'] . ")";
echo $sql; // cg 表插入
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询