我有一些
PHP和SQL的问题,
PHP对我来说是新的,我缺乏SQL.
我想在我的数据库的表中添加值:值可以从表单中获取,也可以从另一个表中获取(作为FK).
这是数据库:
create database AAA;
use AAA;
create table assure(
id_assure varchar(13) not null,
nom varchar(20),
adresse varchar(50), mdp varchar(60),
primary key(id_assure));
create table vehicule(
id_vehicule varchar(13) not null,
immatriculation varchar(7),
masse int(4),
volume int(4),
id_assure varchar(13) not null,
primary key(id_vehicule),
foreign key(id_assure) references assure(id_assure));
create table reparation(
id_reparation varchar(13) not null,
libelle varchar(100),
couts int(5),
primary key(id_reparation),
id_vehicule varchar(13) not null,
foreign key(id_vehicule) references vehicule(id_vehicule));
create table sinistre(
id_sinistre varchar(13) not null,
libelle varchar(100),
date_sinistre date,
heure_sinistre time,
primary key(id_sinistre),
id_vehicule varchar(13) not null,
foreign key(id_vehicule) references vehicule(id_vehicule));
create table dossier(
id_dossier varchar(13) not null,
primary key(id_dossier),
id_sinistre varchar(13) not null,
foreign key(id_sinistre) references sinistre(id_sinistre));
create table contrat_assurance(
id_assurance varchar(13) not null,
primary key(id_assurance),
id_assure varchar(13) not null,
id_vehicule varchar(13) not null,
foreign key(id_assure) references assure(id_assure),
foreign key(id_vehicule) references vehicule(id_vehicule));
create table type_garantie(
code_garantie varchar(13) not null,
libelle varchar(100),
franchise int(3) ,
primary key(code_garantie),
id_assurance varchar(13) not null,
id_reparation varchar(13) not null,
foreign key(id_reparation) references reparation(id_reparation),
foreign key(id_assurance) references contrat_assurance(id_assurance));
DELIMITER //
CREATE TRIGGER trigfranchise
BEFORE INSERT ON type_garantie
for EACH ROW
BEGIN
IF NEW.franchise not between 150 and 600
THEN
SET NEW.franchise = NULL ;
END IF;
END ;//
DELIMITER ;
DELIMITER //
CREATE TRIGGER trigfranchise1
BEFORE UPDATE ON type_garantie
for EACH ROW
BEGIN
IF NEW.franchise not between 150 and 600
THEN
SET NEW.franchise = OLD.franchise ;
END IF;
END ;//
DELIMITER ;
我的第一个表单发送值以确保表,它工作正常.
我的第二个意思是将值发送到vehicule和contrat_assurance表;检查表单后,浏览器显示:
Erreur : SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot
add or update a child row: a foreign key constraint fails
(aaa
.vehicule
, CONSTRAINTvehicule_ibfk_1
FOREIGN KEY
(id_assure
) REFERENCESassure
(id_assure
))
我的PHP代码如下:
try
{
$nom=mysql_real_escape_string($_POST["nom"]);
$mdp=mysql_real_escape_string($_POST["mdp"]);
$imma=mysql_real_escape_string($_POST["immatriculation"]);
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$bdd = new PDO('mysql:host=localhost;dbname=AAA', 'root', '', $pdo_options);
$req = $bdd->prepare('INSERT INTO vehicule (id_vehicule, immatriculation) VALUES(:id_vehicule, :immatriculation)');
$req->execute(array(
'id_vehicule' => uniqid(),
'immatriculation' => $imma
));
$req2 = $bdd->prepare("INSERT INTO vehicule (id_assure) SELECT id_assure FROM assure WHERE nom =\"$nom\" AND mdp =\"$mdp\"" );
$req3 = $bdd->prepare('INSERT INTO contrat_assurance (id_assurance) VALUES(:id_assurance)');
$req4 = $bdd->prepare("INSERT INTO contrat_assurance (id_assure, id_vehicule) SELECT id_assure, id_vehicule FROM vehicule WHERE immatriculation =\"$imma\"");
$req3->execute(array(
'id_assurance' => uniqid()
));
}
实际上,我找到了关于这个错误的其他主题,由于我的技能很差,我无法将答案与我的问题相匹配.
对不起英文和法文表名不佳.
谢谢你的阅读!
更新:我试图通过终端(没有PHP)添加值,它工作,因此我的PHP代码是错误的.
UPDATE2:这是将id_assure插入到保证表(它是vehicule表中的FK)的PHP代码:
try
{
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$bdd = new PDO('mysql:host=localhost;dbname=AAA', 'root', '', $pdo_options);
$req = $bdd->prepare('INSERT INTO assure(id_assure, nom, adresse, mdp) VALUES(:id_assure, :nom, :adresse, :mdp)');
$req->execute(array(
'id_assure' => uniqid(),
'nom' => $_POST['nom'],
'adresse' => $_POST['adresse'],
'mdp' => $_POST['mdp']
));
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
最佳答案 问题是,当您插入到vehicule时,您需要指定外键,即您需要将值插入其中,并且该值需要存在于引用表中.在这种形式中,您插入的唯一列是id_vehicule和immatriculation,这意味着id_assure将保持为空,这意味着约束将失败.