当我提交表单时,我一直收到这样的错误。我不知道我在这里做了什么究竟是错的。
请在下面查看我的代码:
SQLSTATE [23000]:完整性约束违规:1048 Le champ'r_maritalstatus'nepeutêtrevide(null)
在英文中,该消息显示“列'r_maritalstatus'可能不为空(null)。”
如果我在r_maritalstatus
中选择一个值,则会显示其他字段的错误。
以下是此代码:
if(!isset($error)){
try {
$stmt = $db->prepare('INSERT INTO resumes (memberID, r_name,r_email,r_mobile,r_address,r_dob,r_maritalstatus,r_nationality,r_religion,r_sex,r_objective,r_languagesknown,r_hobbies,r_expcategory,r_exptype,r_exptitle1,r_expcompany1,r_expcity1,r_expfrom1,r_expto1,r_expdescription1,r_exptitle2,r_expcompany2,r_expcity2,r_expfrom2,r_expto2,r_expdescription2,r_exptitle3,r_expcompany3,r_expcity3,r_expfrom3,r_expto3,r_expdescription3,r_exptitle4,r_expcompany4,r_expcity4,r_expfrom4,r_expto4,r_expdescription4,r_exptitle5,r_expcompany5,r_expcity5,r_expfrom5,r_expto5,r_expdescription5,r_eduinstname1,r_edudegree1,r_edustudyfield1,r_educity1,r_edupassed1,r_eduinstname2,r_edudegree2,r_edustudyfield2,r_educity2,r_edupassed2,r_eduinstname3,r_edudegree3,r_edustudyfield3,r_educity3,r_edupassed3,r_eduinstname4,r_edudegree4,r_edustudyfield4,r_educity4,r_edupassed4,r_eduinstname5,r_edudegree5,r_edustudyfield5,r_educity5,r_edupassed5,r_skill1,r_skillexp1,r_skill2,r_skillexp2,r_skill3,r_skillexp3,r_skill4,r_skillexp4,r_skill5,r_skillexp5) VALUES (:memberID, :r_name, :r_email, :r_mobile, :r_address, :r_dob, :r_maritalstatus, :r_nationality, :r_religion, :r_sex, :r_objective, :r_languagesknown, :r_hobbies, :r_expcategory, :r_exptype, :r_exptitle1, :r_expcompany1, :r_expcity1, :r_expfrom1, :r_expto1, :r_expdescription1, :r_exptitle2, :r_expcompany2, :r_expcity2, :r_expfrom2, :r_expto2, :r_expdescription2, :r_exptitle3, :r_expcompany3, :r_expcity3, :r_expfrom3, :r_expto3, :r_expdescription3, :r_exptitle4, :r_expcompany4, :r_expcity4, :r_expfrom4, :r_expto4, :r_expdescription4, :r_exptitle5, :r_expcompany5, :r_expcity5, :r_expfrom5, :r_expto5, :r_expdescription5, :r_eduinstname1, :r_edudegree1, :r_edustudyfield1, :r_educity1, :r_edupassed1, :r_eduinstname2, :r_edudegree2, :r_edustudyfield2, :r_educity2, :r_edupassed2, :r_eduinstname3, :r_edudegree3, :r_edustudyfield3, :r_educity3, :r_edupassed3, :r_eduinstname4, :r_edudegree4, :r_edustudyfield4, :r_educity4, :r_edupassed4, :r_eduinstname5, :r_edudegree5, :r_edustudyfield5, :r_educity5, :r_edupassed5, :r_skill1, :r_skillexp1, :r_skill2, :r_skillexp2, :r_skill3, :r_skillexp3, :r_skill4, :r_skillexp4, :r_skill5, :r_skillexp5)');
$stmt->execute(array(
':memberID' => $uid,
':r_name' => $rname,
':r_email' => $remail,
':r_mobile' => $rmobile,
':r_address' => $raddress,
':r_dob' => $rdob,
':r_sex' => $rsex,
':r_maritalstatus' => $rmaritalstatus,
':r_nationality' => $rnationality,
':r_religion' => $rreligion,
':r_objective' => $robjective,
':r_languagesknown' => $rlanguagesknown,
':r_hobbies' => $rhobbies,
':r_expcategory' => $rjobcategory,
':r_exptype' => $rjobtype,
':r_exptitle1' => $rjobtitle1,
':r_expcompany1' => $rjobcompany1,
':r_expcity1' => $rjobcity1,
':r_expfrom1' => $rjobfrom1,
':r_expto1' => $rjobto1,
':r_expdescription1' => $rjobdescription1,
':r_exptitle2' => $rjobtitle2,
':r_expcompany2' => $rjobcompany2,
':r_expcity2' => $rjobcity2,
':r_expfrom2' => $rjobfrom2,
':r_expto2' => $rjobto2,
':r_expdescription2' => $rjobdescription2,
':r_exptitle3' => $rjobtitle3,
':r_expcompany3' => $rjobcompany3,
':r_expcity3' => $rjobcity3,
':r_expfrom3' => $rjobfrom3,
':r_expto3' => $rjobto3,
':r_expdescription3' => $rjobdescription3,
':r_exptitle4' => $rjobtitle4,
':r_expcompany4' => $rjobcompany4,
':r_expcity4' => $rjobcity4,
':r_expfrom4' => $rjobfrom4,
':r_expto4' => $rjobto4,
':r_expdescription4' => $rjobdescription4,
':r_exptitle5' => $rjobtitle5,
':r_expcompany5' => $rjobcompany5,
':r_expcity5' => $rjobcity5,
':r_expfrom5' => $rjobfrom5,
':r_expto5' => $rjobto5,
':r_expdescription5' => $rjobdescription5,
':r_eduinstname1' => $reduinst1,
':r_edudegree1' => $redudegree1,
':r_edustudyfield1' => $redustudyfield1,
':r_educity1' => $reducity1,
':r_edupassed1' => $reduyear1,
':r_eduinstname2' => $reduinst2,
':r_edudegree2' => $redudegree2,
':r_edustudyfield2' => $redustudyfield2,
':r_educity2' => $reducity2,
':r_edupassed2' => $reduyear2,
':r_eduinstname3' => $reduinst3,
':r_edudegree3' => $redudegree3,
':r_edustudyfield3' => $redustudyfield3,
':r_educity3' => $reducity3,
':r_edupassed3' => $reduyear3,
':r_eduinstname4' => $reduinst4,
':r_edudegree4' => $redudegree4,
':r_edustudyfield4' => $redustudyfield4,
':r_educity4' => $reducity4,
':r_edupassed4' => $reduyear4,
':r_eduinstname5' => $reduinst5,
':r_edudegree5' => $redudegree5,
':r_edustudyfield5' => $redustudyfield5,
':r_educity5' => $reducity5,
':r_edupassed5' => $reduyear5,
':r_skill1' => $rskill1,
':r_skillexp1' => $rskillexp1,
':r_skill2' => $rskill2,
':r_skillexp2' => $rskillexp2,
':r_skill3' => $rskill3,
':r_skillexp3' => $rskillexp3,
':r_skill4' => $rskill4,
':r_skillexp4' => $rskillexp4,
':r_skill5' => $rskill5,
':r_skillexp5' => $rskillexp5
));
} catch(PDOException $e) {
$error[] = $e->getMessage();
}
}
echo print_r($_POST);
}
感谢和问候
答案 0 :(得分:2)
在数据库架构中,您将r_maritalstatus
列定义为NOT NULL
。这意味着无论何时执行INSERT
,您都必须将一些值传递给此列,您不能将其省略或设置为NULL
。
<强>解决方案强>
如果您需要始终设置r_maritalstatus
,这意味着NOT NULL
约束是正确的,并且它会停止操作以警告您。在var_dump($rmaritalstatus);
之前执行->execute
,它很可能是NULL
。因此,解决方案是在执行INSERT之前验证此变量,您始终从简历表单中获取值。
如果您不关心r_maritalstatus
总是有值,则表示您需要删除约束。只需转到数据库工具,将此字段更改为NULL
,而不是NOT NULL
。
如果您这样做,但是您在许多其他字段中遇到了同样的问题而且您已经厌倦了将它们更改为NULL
,那么这个代码最有可能在< strong>非严格的MySQL模式。在这种模式下,MySQL会忽略NOT NULL
并将省略的字符串字段设置为空,将数字字段设置为0. 建议仅在您急需修复突然断开的应用程序时才执行以下操作。移动到另一台服务器或将MySQL更新到更新的版本。更改my.cnf文件以使用此行:sql_mode="NO_ENGINE_SUBSTITUTION"
(最不严格的模式)。重启mysql服务器,然后重试。此外,如果您只想为应用程序更改它,可以在使用$db = new PDO(...)
连接到数据库后立即添加这些:
$db->exec('SET SESSION sql_mode="NO_ENGINE_SUBSTITUTION"');
$db->exec('SET GLOBAL sql_mode="NO_ENGINE_SUBSTITUTION"');
<强>症状:强>
当开发人员在非严格模式下创建数据库模式并且没有注意到他们默认使用的数据库管理工具(例如phpMyAdmin)设置的NOT NULL
时,通常会发生这种情况。然后他们将它移动到服务器或升级到更新版本的MySQL,并想知道为什么他们突然收到许多错误,如'1048 Integrity constant violation'或'1364 Field没有默认值'。通常它与改变的模式有关。