当我使用 MYSQL_INSERT_ID 时出现错误。
错误 :
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在 'INSERT INTO FicheHandicap (NumeroCORH) VALUES (NULL); 附近使用的正确语法;SET @NewidFicheHandicap =' 在第 2 行
请求:
START TRANSACTION;
INSERT INTO FicheHandicap (NumeroCORH) VALUES (NULL);
SET @NewidFicheHandicap = LAST_INSERT_ID();
INSERT INTO FicheSanitaire (DateVaccinHepatite) VALUES (NULL);
SET @NewidFicheSanitaire = LAST_INSERT_ID();
INSERT INTO Identite (Nom,NomJeuneFille,Prenoms,DateNaissance,LieuNaissance,Nationalite,CouvertureSociale,Mutuelle1,Mutuelle2,Mutuelle3,Bourse1,Bourse2,Sexe,idGenre,idFicheSanitaire,idFicheHandicap) VALUES ("TOTO","","TOTO","2013-06-12","TOTO","TOTO","TOTO","TOTO","TOTO","TOTO","TOTO","TOTO","0","1",@NewidFicheSanitaire,@NewidFicheHandicap);
SET @NewidIdentite = LAST_INSERT_ID();
INSERT INTO Personne (MembreActif, Actif, IdIdentite) VALUES ("1", "1", @NewidIdentite);
COMMIT;
我与 php 一起使用:
$query = 'START TRANSACTION;'."\n";
$query .= ' INSERT INTO FicheHandicap (NumeroCORH) VALUES (NULL);'."\n";
$query .= ' SET @NewidFicheHandicap = LAST_INSERT_ID();'."\n";
$query .= ' INSERT INTO FicheSanitaire (DateVaccinHepatite) VALUES (NULL);'."\n";
$query .= ' SET @NewidFicheSanitaire = LAST_INSERT_ID();'."\n";
$query .= ' INSERT INTO Identite (Nom,NomJeuneFille,Prenoms,DateNaissance,LieuNaissance,Nationalite,CouvertureSociale,Mutuelle1,Mutuelle2,Mutuelle3,Bourse1,Bourse2,Sexe,idGenre,idFicheSanitaire,idFicheHandicap) VALUES ("'.$resultat['Nom'].'","'.$resultat['NomJeuneFille'].'","'.$resultat['Prenoms'].'","'.preg_replace("#(.*)/(.*)/(.*)#", "$3-$2-$1", $resultat['DateNaissance']).'","'.$resultat['LieuNaissance'].'","'.$resultat['Nationalite'].'","'.$resultat['CouvertureSociale'].'","'.$resultat['Mutuelle1'].'","'.$resultat['Mutuelle2'].'","'.$resultat['Mutuelle3'].'","'.$resultat['Bourse1'].'","'.$resultat['Bourse2'].'","'.$resultat['Sexe'].'","'.$resultat['idGenre'].'",@NewidFicheSanitaire,@NewidFicheHandicap);'."\n";
$query .= ' SET @NewidIdentite = LAST_INSERT_ID();'."\n";
$query .= ' INSERT INTO Personne (MembreActif, Actif, IdIdentite) VALUES ("'.$resultat['MembreActif'].'", "'.$resultat['Actif'].'", @NewidIdentite);'."\n";
$query .= ' COMMIT;'."\n";
$resultat = $mysqli->query($query);