Real example:
/*************************by garcon1986*********************************************************/ <?php // 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 '<h3>LES INFORMATION DE L/'ENTREPRISE</h3>'; echo '<table style="width:80%; margin: auto 0">'; echo '<th style="width:40%"></th><th stype="width:60%"></th>'; echo '<tr class="line_even" >'; echo '<td>Entreprise :</td>'; echo '<td>'.$nom.'</td>'; echo '</tr>'; echo '<tr class="line_odd">'; echo '<td>Son secteur : </td>'; echo '<td>'.$secteur.'</td>'; echo '</tr>'; echo '<tr class="line_even">'; echo '<td>Son taille : </td>'; echo '<td>'.$taille.'</td>'; echo '</tr>'; echo '<tr class="line_odd">'; echo '<td>Son concurrent : </td>'; echo '<td>'.$concurrent.'</td>'; echo '</tr>'; echo '<tr class="line_even">'; echo '<td>Son concurrent : </td>'; echo '<td>'.$investisseur.'</td>'; echo '</tr>'; echo '<tr class="line_odd">'; echo '<td>Son partenaire : </td>'; echo '<td>'.$partenaire.'</td>'; echo '</tr>'; echo '<tr class="line_even">'; echo '<td>Son fournisseur : </td>'; echo '<td>'.$fournisseur.'</td>'; echo '</tr>'; echo '<tr class="line_odd">'; echo '<td>Son client : </td>'; echo '<td>'.$client.'</td>'; echo '</tr>'; echo '<tr class="line_even">'; echo '<td>Son information generale : </td>'; echo '<td>'.$info_general.'</td>'; echo '</tr>'; echo '</table>'; ?>
多个表格同时插入时,需要使用事务。
<?php //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'); ?>