mysql同时多表插入_MySQL多表同时插入
发布日期:2021-06-24 17:54:45 浏览次数:3 分类:技术文章

本文共 3856 字,大约阅读时间需要 12 分钟。

Real example:

/*************************by garcon1986*********************************************************/

// get the values from entreprise creation form

$secteur = $_POST['secteur'];

$nom = $_POST['nom_entreprise'];

$taille = $_POST['taille_entreprise'];

$concurrent = $_POST['concurrent_entreprise'];

$investisseur = $_POST['investisseur_entreprise'];

$partenaire = $_POST['partenaire_entreprise'];

$client = $_POST['client_entreprise'];

$fournisseur = $_POST['fournisseur_entreprise'];

$info_general = $_POST['information_generale'];

//connection

//include("./conn/connect.php");

$conn = mysql_connect("localhost","charles","charles") or die("connection failed: ".mysql_error());

mysql_select_db("crm") or die("select db failed: ".mysql_error());

//execution - using trasaction

mysql_query('START TRANSACTION',$conn) or die(mysql_error());

mysql_query("insert into entreprise(nom, nombre_salarie, secteur, info_general) values('".$nom."','".$taille."','".$secteur."','".$info_general."')",$conn) or die(mysql_error());

$id = mysql_insert_id($conn) or die(mysql_error());

mysql_query("insert into concurrent(entreprise_id, nom) values('".$id."', '".$concurrent."')",$conn) or die(mysql_error());

mysql_query("insert into investisseur(entreprise_id, nom) values('".$id."', '".$investisseur."')",$conn) or die(mysql_error());

mysql_query("insert into partenaire(entreprise_id, nom) values('".$id."', '".$partenaire."')",$conn) or die(mysql_error());

mysql_query("insert into client(entreprise_id, nom) values('".$id."', '".$client."')",$conn) or die(mysql_error());

mysql_query("insert into fournisseur(entreprise_id, nom) values('".$id."', '".$fournisseur."')",$conn) or die(mysql_error());

mysql_query('COMMIT')or die(mysql_error());

//display the information

echo '

LES INFORMATION DE L/'ENTREPRISE

';

echo '

echo '

';

echo '

';

echo '

Entreprise :';

echo '

'.$nom.'';

echo '

';

echo '

';

echo '

Son secteur : ';

echo '

'.$secteur.'';

echo '

';

echo '

';

echo '

Son taille : ';

echo '

'.$taille.'';

echo '

';

echo '

';

echo '

Son concurrent : ';

echo '

'.$concurrent.'';

echo '

';

echo '

';

echo '

Son concurrent : ';

echo '

'.$investisseur.'';

echo '

';

echo '

';

echo '

Son partenaire : ';

echo '

'.$partenaire.'';

echo '

';

echo '

';

echo '

Son fournisseur : ';

echo '

'.$fournisseur.'';

echo '

';

echo '

';

echo '

Son client : ';

echo '

'.$client.'';

echo '

';

echo '

';

echo '

Son information generale : ';

echo '

'.$info_general.'';

echo '

';

echo '

';

?>

多个表格同时插入时,需要使用事务。

//mysql code

/*

create table table1(id int(10) not null auto_increment, name varchar(20), primary key(id));

create table table2(id int(10) not null auto_increment, t1id int(10), name varchar(20), primary key(id));

create table table3(id int(10) not null auto_increment, t1id int(10), name varchar(20), primary key(id));

alter table table2 add foreign key (t1id) references table1(id) on delete cascade;

alter table table3 add foreign key (t1id) references table1(id) on delete cascade;

insert into table1(name) values('t1_name');

insert into table2(t1id, name) values(last_insert_id(), 't2_name');

insert into table3(t1id, name) values(last_insert_id(), 't3_name');

*/

//php实现

error_reporting(E_ALL ^ E_NOTICE);

$conn = mysql_connect("localhost","charles","charles");

mysql_select_db('test');

$link = 0;

mysql_query('START TRANSACTION',$link);

mysql_query("insert into table1(name) values('t1_name')",$link);

$id = mysql_insert_id($link);

mysql_query("insert into table2(t1id, name) values($id, 't2_name')",$link);

mysql_query("insert into table3(t1id, name) values($id, 't3_name')",$link);

mysql_query('COMMIT');

//mysql 实现

set @temp=0;

insert into table1(name) values('t1_name');

select mysql_insert_id() into @temp;

insert into table2(t1id, name) values(@temp, 't2_name');

insert into table3(t1id, name) values(@temp, 't3_name');

?>

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2010-02-27 00:47

浏览 511

评论

转载地址:https://blog.csdn.net/weixin_34237125/article/details/113605496 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:postman delete 请求传递数组_Postman请求方法
下一篇:lua 区间比较_TI-Lua 系列教程2.4.1: 条件分支

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年03月30日 23时07分20秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

【英语学习】【Daily English】U08 Dating L01 She is the one for me. 2019-04-28
【英语学习】【WOTD】orthography 释义/词源/示例 2019-04-28
【英语学习】【Daily English】U08 Dating L02 What would you do if you were me? 2019-04-28
【英语学习】【WOTD】canker 释义/词源/示例 2019-04-28
【英语学习】【WOTD】polyglot 释义/词源/示例 2019-04-28
【Python】Python3.7.3 - Python命令行参数详解 2019-04-28
【英语学习】【WOTD】comminute 释义/词源/示例 2019-04-28
【英语学习】【Daily English】U09 Fashion L04 It helps if you look the part 2019-04-28
【英语学习】【WOTD】scrumptious 释义/词源/示例 2019-04-28
【英语学习】【WOTD】etiquette 释义/词源/示例 2019-04-28
【英语学习】【Daily English】U10 Education L01 Is this certificate a must? 2019-04-28
【Openstack】【Nova】开发者入门,开发工作流 2019-04-28
【英语学习】【Daily English】U10 Education L02 I'm not a pushy parent 2019-04-28
【英语学习】【Daily English】U10 Education L03 She's planning to study abroad 2019-04-28
【英语学习】【Daily English】U10 Education L04 It's never too late to learn. 2019-04-28
【英语学习】【WOTD】sashay 释义/词源/示例 2019-04-28
【Openstack】实录手动部署Openstack Rocky 双节点(3)- Glance 2019-04-28
【Openstack】实录手动部署Openstack Rocky 双节点(4)- Nova 2019-04-28
【英语学习】【WOTD】cerebral 释义/词源/示例 2019-04-28
【英语学习】【Daily English】U11 Work L01 Would you like a tour of the office? 2019-04-28