我有两个表main_jobs和sub_jobs,结构如下:
$query="create table if not exists main_jobs (
id int not null auto_increment, primary key(id),
industry int(3),
company_name varchar(255),
job_title varchar(255),
email varchar(255),
website varchar(255),
introduction text not null,
application_details text,
advert_date date,
expiry_date date,
upload_date date,
no_deadline int(1) default 0,
logo varchar(255),
featured varchar(20),
source varchar(10) default 'admin',
email_status int default 0,
views int(11) default 1,
short_url varchar(100),
tags varchar(255),
FOREIGN KEY (industry) REFERENCES industry (id))";
if(mysql_query($query,$link)){echo "main_jobs created<br>";} else{ die(mysql_error()); }
$query="create table if not exists sub_jobs (
id int not null auto_increment, primary key(id),
parent_id int(11) not null, FOREIGN KEY (parent_id) REFERENCES main_jobs (id),
title varchar(255),
description text not null,
category int (3), FOREIGN KEY (category) REFERENCES category (id),
job_type varchar(20),
job_level varchar(50),
min_qualification varchar(50),
min_experience int(3),
max_experience int(3),
min_salary int(11),
max_salary int(11),
show_salary int(1) default 1,
denomination varchar(10),
views int(11) default 1,
short_url varchar(100),
email varchar(255),
website varchar(255))";
if(mysql_query($query,$link)){echo "sub_jobs created<br>";} else{ die(mysql_error()); }
我想插入记录,但它会显示此错误:
Cannot add or update a child row: a foreign key constraint fails (`myjobmag_db`.`sub_jobs`, CONSTRAINT `sub_jobs_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `main_jobs` (`id`))
这些是mysql查询,并且已经盯着它们好几个小时但无法识别问题:
插入main_jobs(成功运行)
$resultobj=otherquery("insert into main_jobs(industry, company_name, job_title, email, website, introduction, application_details, advert_date, expiry_date, upload_date, no_deadline, logo) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", "ssssssssssss", array($industry, $company_name, $job_name, $email, $website, $profile, $application, $advert_date, $expiry_date, $date_uploaded, $no_deadline, $logo));
我选择了最后一个插入的id(确认它存在于main_jobs表中,这是因为错误而手动执行)并运行insert into sub_jobs
$parent= $resultobj['obj']->insert_id;
mysql_query("insert into sub_jobs(id, parent_id, title, description, category, job_type, job_level, min_qualification, min_experience, max_experience, min_salary, max_salary, show_salary, denomination, email, website) values('', $parent, '$subtitle', '$description', '$category', '$type', '$level', '$min_qualification', '$min_experience', '$max_experience', '$min_salary', '$max_salary', '$show_salary', '$denomination', '$sub_email', '$sub_website')", $link) or die(mysql_error($link));
在我的上一次测试中,main_jobs表中的id是2616,它确实存在,但是我收到错误。
请帮助!