I got two tables of database
CREATE TABLE `articles` (
`id_a` int(11) not null,
`name` varchar(70) not null,
`text` text not null,
`datum` timestamp not null,
`id_c` int(11) not null,
`id_u` int(11) not null )
ENGINE=InnoDB;
and
CREATE TABLE `category` (
`id_c` int(11) not null,
`name` varchar(70) not null )
ENGINE=InnoDB;
i got one realtion there
ALTER TABLE `articles`
ADD CONSTRAINT `fk_ArtCat`
FOREIGN KEY (`id_c`)
REFERENCES `category` (`id_c`);
code for insert articles to database
$name = $_POST['name'];
$text = mysqli_real_escape_string ($connect, $_POST['edit']);
$cat = $_POST['category'];
$catid = "SELECT id_c FROM category WHERE name = $cat";
$sql = "INSERT INTO articles (id_a, name, text, datum, id_c, id_u) VALUES ('', '$name', '$text', current_timestamp, '$catid', '1')";
Can you tell me why I got an error when I tried to post a article? This err I think:
Cannot add or update a child row: a foreign key constraint fails (db_news
.articles
, CONSTRAINT fk_ArtCat
FOREIGN KEY (id_c
) REFERENCES category
(id_c
))