0

This is the error i get Cannot add or update a child row:

a foreign key constraint fails (selo.klijent, CONSTRAINT klijent_ibfk_1 FOREIGN KEY (GradID) REFERENCES grad (GradID))

this is my insert query

 string insertQuery = " INSERT INTO selo.Klijent(KlijentID,Ime,Prezime,Adresa,GradID,Telefon,Email,AktivanKlijent) Values('" + TB_Sifra + "','" + TB_Ime.Text + "','" + TB_Prezime.Text + "','" + TB_Adresa.Text + "','" + CB_Gradovi + "','" + TB_Telefon + "','" + TB_Mail.Text + "','" + proveraRB() + "')";

and this is my mysql code

create table Klijent(

KlijentID INT NOT NULL AUTO_INCREMENT primary key,
Ime varchar(20) not null,
Prezime varchar(20) not null,
Adresa varchar(20) not null,
GradID  INT NOT NULL,
Telefon int not null,
Email varchar(20),
AktivanKlijent varchar(2),

FOREIGN KEY (GradID) REFERENCES Grad(GradID)
);

Really not sure what to do here

Raizzen
  • 192
  • 13
  • In your grad table, you must have a record with GradID which you are trying to insert to Klijent table. If you already have it, provide more details. – Doruk Apr 27 '18 at 06:58
  • this is gradID from table Grad GradID INT NOT NULL AUTO_INCREMENT, – Ilija Pavlovic Apr 27 '18 at 07:02

3 Answers3

2

You didn't provide an existing GradID. Furthermore you shouldn't pass a KlijentID because that is an autoincrement. Besides, your code can be hacked: better use a parametrized query.

Ole EH Dufour
  • 2,968
  • 4
  • 23
  • 48
2

check Grad table - GradID column have the GradID value

PS.you have to know : How does the SQL injection from the “Bobby Tables” XKCD comic work?

Wei Lin
  • 3,591
  • 2
  • 20
  • 52
0

I didnt convert CB value to int.