1

我有两个表 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,它确实存在,但我得到了一个错误。

请协助!

4

0 回答 0