-1

These are my tables:

create table sender(
    sno varchar(6) not null,
    sfname varchar(15) not null,
    slname varchar(10) not null,
    sphone varchar(10),
    saddress varchar(40) not null,

    constraint pk_sender primary key(sno)
 );


create table courier(
    cno varchar(6),
    cost double precision not null,
    weight double precision not null,
    del_stat varchar(20) not null,
    no_cour int(10),
    sno varchar(6),

    constraint fk_courier foreign key(sno)
    references sender(sno),constraint pk_courier primary key(cno)
 );

And this is my php code:

    <?php
session_start();
$_SESSION['x']=$_POST[sno];
$con=mysqli_connect("localhost","root","project123","project");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$sql1="insert into sender(sno,sfname,slname,sphone,saddress) values (concat('SN','$_POST[sno]'),'$_POST[sf]','$_POST[sl]','$_POST[sph]','$_POST[sad]')";
$sql2="insert into courier(cno,cost,weight,del_stat,no_cour) values (concat('CN','$_POST[cno]'),'$_POST[cst]','$_POST[wght]','$_POST[del]','$_POST[num]')";

if(!mysqli_query($con,$sql1) && !mysqli_query($con,$sql2))
{
    die('Error: ' . mysqli_error($con));
}




mysqli_close($con);
?>

But I'm getting error Error: Cannot add or update a child row: a foreign key constraint fails (project.courier, CONSTRAINT fk_courier FOREIGN KEY (sno) REFERENCES sender (sno))

So please tell me how to insert values to foreign key.

rray
  • 2,518
  • 1
  • 28
  • 38
user2987938
  • 3
  • 1
  • 2
  • http://stackoverflow.com/questions/8728598/cant-insert-foreign-key-value-into-linking-table?rq=1 Perhaps you might find the solution here –  Nov 13 '13 at 13:59

1 Answers1

0

It seems that you're not setting the foreign key value on your child table insert. Your courier table have a column sno VARCHAR(6) that references sender.sno VARCHAR(6) right? So you have to provide it value. In order to relate two fields as foreign key, you have to set a valid value for the child column existing on the parent related column.

Anyway, you should run those two queries like this:

$sno = "SN$_POST[sno]";
$cno = "CN$_POST[cno]";

$sql1="insert into sender(sno,sfname,slname,sphone,saddress) values ('$sno','$_POST[sf]','$_POST[sl]','$_POST[sph]','$_POST[sad]')";

if(mysqli_query($con,$sql1))
{
    $sql2="insert into courier(cno,cost,weight,del_stat,no_cour, sno) values ('$cno','$_POST[cst]','$_POST[wght]','$_POST[del]','$_POST[num]', '$sno')";

    if (mysqli_query($con,$sql2))
    {
        // Success
    }
    else 
    {
        die('Error on query 2: ' . mysqli_error($con));
    }
}
else 
{
    die('Error on query 1: ' . mysqli_error($con));
}

You can see here how to get the last inserted id from an auto_increment field. If you're not using an auto_increment field, you have to select it in other way that fits your needs.

I hope this helps.

UPDATE: I have change those variables sno and cno because you can avoid concatenating it inside your query. You can do that that way. I realize that you already have the sno value, so you don't need to query again after it. That code may now work.

Community
  • 1
  • 1
DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
  • but its showing an error: Warning: extract() expects parameter 1 to be array, boolean given in C:\xamp1\htdocs\DB-UX\new1.php on line 15 Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in C:\xamp1\htdocs\DB-UX\new1.php on line 15 Error on query 2: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 – user2987938 Nov 13 '13 at 16:22