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

本文共 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: 条件分支

发表评论

最新留言

很好
[***.229.124.182]2024年04月02日 01时32分23秒