-1

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))

Leodiruk
  • 1
  • 3
  • I removed the sql-server tag, because the code is obviously MySQL. – Gordon Linoff Mar 03 '16 at 12:28
  • You seem to be putting your SELECT statement into `$catid`, and using that as the value in your INSERT, rather than running the query and extracting the value – andrewsi Mar 03 '16 at 12:31
  • A foreign key constraint fail means you're trying to add an entry into `articles` with an `id_c` that doesn't exist in the `category` table. Double-check your queries and try executing them manually. – Sébastien Vercammen Mar 03 '16 at 12:31
  • 2
    Possible duplicate of [Mysql error 1452 - Cannot add or update a child row: a foreign key constraint fails](http://stackoverflow.com/questions/1253459/mysql-error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fa) – Francesco de Guytenaere Mar 03 '16 at 12:32
  • The category id you are trying to insert does not exist in the category table. – Shadow Mar 03 '16 at 12:32
  • foreign key constraint failed, there is no such category you are entering for this article. – Muhammed Mar 03 '16 at 12:32
  • `id_k` is not exist in `category` table – Divyesh Savaliya Mar 03 '16 at 12:33

1 Answers1

0

You have multitude of issues with your php / sql code.

$catid = "SELECT id_k FROM category WHERE name = $cat";
  1. There is no id_k field in category table
  2. name is a text field, but $cat is not enclosed by single quotes

...VALUES ('', '$name', '$text', current_timestamp, '$catid', '1')...

  1. $catid is an sql command, but you enclose its value by single quotes, making it a string. You are essentially trying to insert the sql command as a text into category id, which will fail.

My suggestion is to change the form that submits the category to this script to send the category id instead of the category name, so you do not have to do a reverse lookup based on the category name to get the id.

Shadow
  • 33,525
  • 10
  • 51
  • 64