view plaincopy to clipboardprint?
1. /*************************by garcon1986*********************************************************/
2. <?php
3. // get the values from entreprise creation form
4. $secteur = $_POST['secteur'];
5. $nom = $_POST['nom_entreprise'];
6. $taille = $_POST['taille_entreprise'];
7. $concurrent = $_POST['concurrent_entreprise'];
8. $investisseur = $_POST['investisseur_entreprise'];
9. $partenaire = $_POST['partenaire_entreprise'];
10. $client = $_POST['client_entreprise'];
11. $fournisseur = $_POST['fournisseur_entreprise'];
12. $info_general = $_POST['information_generale'];
13. //connection
14. //include("./conn/connect.php");
15. $conn = mysql_connect("localhost","charles","charles") or die("connection failed: ".mysql_error());
16. mysql_select_db("crm") or die("select db failed: ".mysql_error());
17. //execution - using trasaction
18. mysql_query('START TRANSACTION',$conn) or die(mysql_error());
19. mysql_query("insert into entreprise(nom, nombre_salarie, secteur, info_general) values('".$nom."','".$taille."','".$secteur."','".$info_general."')",$conn) or die(mysql_error());
20. $id = mysql_insert_id($conn) or die(mysql_error());
21. mysql_query("insert into concurrent(entreprise_id, nom) values('".$id."', '".$concurrent."')",$conn) or die(mysql_error());
22. mysql_query("insert into investisseur(entreprise_id, nom) values('".$id."', '".$investisseur."')",$conn) or die(mysql_error());
23. mysql_query("insert into partenaire(entreprise_id, nom) values('".$id."', '".$partenaire."')",$conn) or die(mysql_error());
24. mysql_query("insert into client(entreprise_id, nom) values('".$id."', '".$client."')",$conn) or die(mysql_error());
25. mysql_query("insert into fournisseur(entreprise_id, nom) values('".$id."', '".$fournisseur."')",$conn) or die(mysql_error());
26. mysql_query('COMMIT')or die(mysql_error());
27. //display the information
28. echo '
LES INFORMATION DE L\'ENTREPRISE
';29. echo '
30. echo '
';31. echo '
';32. echo '
Entreprise :';33. echo '
'.$nom.'';34. echo '
';35. echo '
';36. echo '
Son secteur : ';37. echo '
'.$secteur.'';38. echo '
';39. echo '
';40. echo '
Son taille : ';41. echo '
'.$taille.'';42. echo '
';43. echo '
';44. echo '
Son concurrent : ';45. echo '
'.$concurrent.'';46. echo '
';47. echo '
';48. echo '
Son concurrent : ';49. echo '
'.$investisseur.'';50. echo '
';51. echo '
';52. echo '
Son partenaire : ';53. echo '
'.$partenaire.'';54. echo '
';55. echo '
';56. echo '
Son fournisseur : ';57. echo '
'.$fournisseur.'';58. echo '
';59. echo '
';60. echo '
Son client : ';61. echo '
'.$client.'';62. echo '
';63. echo '
';64. echo '
Son information generale : ';65. echo '
'.$info_general.'';66. echo '
';67. echo '
';68. ?>
利用mysql_insert_id()函数可以返回上一步insert操作所返回的ID,这个时候可以用来执行先插入主表,再插入子表
1. <?php
2. //mysql code
3. /*
4. create table table1(id int(10) not null auto_increment, name varchar(20), primary key(id));
5. create table table2(id int(10) not null auto_increment, t1id int(10), name varchar(20), primary key(id));
6. create table table3(id int(10) not null auto_increment, t1id int(10), name varchar(20), primary key(id));
7. alter table table2 add foreign key (t1id) references table1(id) on delete cascade;
8. alter table table3 add foreign key (t1id) references table1(id) on delete cascade;
9. insert into table1(name) values('t1_name');
10. insert into table2(t1id, name) values(last_insert_id(), 't2_name');
11. insert into table3(t1id, name) values(last_insert_id(), 't3_name');
12. */
13. //php实现
14. error_reporting(E_ALL ^ E_NOTICE);
15. $conn = mysql_connect("localhost","charles","charles");
16. mysql_select_db('test');
17. $link = 0;
18. mysql_query('START TRANSACTION',$link);
19. mysql_query("insert into table1(name) values('t1_name')",$link);
20. $id = mysql_insert_id($link);
21. mysql_query("insert into table2(t1id, name) values($id, 't2_name')",$link);
22. mysql_query("insert into table3(t1id, name) values($id, 't3_name')",$link);
23. mysql_query('COMMIT');
24.
25. //mysql 实现
26. set @temp=0;
27. insert into table1(name) values('t1_name');
28. select mysql_insert_id() into @temp;
29. insert into table2(t1id, name) values(@temp, 't2_name');
30. insert into table3(t1id, name) values(@temp, 't3_name');
31. ?>
分享到:
2010-11-17 19:26
浏览 3402
评论