下面的代碼(reg_org.php)明顯的事情是,登記要求:外鍵沒有在子表中添加一行,雖然被照顧的
<?php
session_start();
if(!empty($_POST))
{
$msg="";
if(empty($_POST['org_name']) || empty($_POST['org_domain']) || empty($_POST['org_admin_name']) || empty($_POST['org_admin_email']) || empty($_POST['org_admin_pass1']) || empty($_POST['org_admin_pass2']))
{
$msg.="<li>Please full fill all requirement";
}
if($_POST['org_admin_pass1']!=$_POST['org_admin_pass2'])
{
$msg.="<li>Please Enter your Password Again.....";
}
if(!ereg("^[a-z0-9_]+[a-z0-9_.]*@[a-z0-9_-]+[a-z0-9_.-]*\.[a-z]{2,5}$",$_POST['org_admin_email']))
{
$msg.="<li>Please Enter Your Valid Email Address...";
}
if(strlen($_POST['org_admin_pass1'])>20)
{
$msg.="<li>Please Enter Your Password in limited Format....";
}
if(is_numeric($_POST['org_name']) || is_numeric($_POST['org_admin_pass1']))
{
$msg.="<li>Names must be in String Format...";
}
if($msg!="")
{
header("location:index.php?error=".$msg);
}
else
{
$orgname = $_POST['org_name'];
$orgdmn = $_POST['org_domain'];
$orgadmin = $_POST['org_admin_name'];
$orgadminmail = $_POST['org_admin_email'];
$orgadminpass = $_POST['org_admin_pass1'];
$link=mysql_connect("localhost","root","")or die("Can't Connect...");
mysql_select_db("educon",$link) or die("Can't Connect to Database...");
$query="insert into institutes(inst_nm,inst_dmn)
values('$orgname','$orgdmn')";
$res = mysql_query($query,$link) or die("Can't Execute Query... ".mysql_error());
$query="insert into users(u_name,u_pwd,u_email,u_type)
values('$orgadmin','$orgadminpass','$orgadminmail','admin')";
$res = mysql_query($query,$link) or die("Can't Execute Query... ".mysql_error());
$_SESSION=array();
$_SESSION['unm']=$orgadmin;
$_SESSION['type']="admin";
$_SESSION['status']=true;
header('location:profile_admin.php');
}
}
else
{
header("location:index.php");
}
?>
在代碼中,根據規則父表(研究所)是在子表(用戶)之前創建的,子列與父列完全相同,但我面臨以下問題:
無法執行查詢...無法添加或更新子項行:外鍵約束失敗(
educon
。users
,CONSTRAINTusers_ibfk_1
FOR橫生KEY(inst_id
)參考文獻institutes
(inst_id
)ON DELETE CASCADE ON UPDATE CASCADE)
我很抱歉,排在父表在子表之前在父表中創建!而不是在子表之前創建父表。 –