Click here to Skip to main content
14,644,614 members
Rate this:
Please Sign up or sign in to vote.
See more:
getting error althogh there is no record in table
i need to insert clgid in couse and seat table bt i m gettig error on primary key .

$query="insert into clgmaster (ClgId,ClgName,ClgAddrs,FeeAmt,CntNo,Email,Pwd) values ($id,'$clgnm','$addrs',$fees,$cntno,'$eml','$pwd')";

    $result = mysql_query($query);//Excecuting clg query

    echo $result."<br>";

    echo "helllo query <br>";
    if ($result)
    {
        echo "<br>You Have Successfully REGISTERED this Record!";

    }

    $fetchData=mysql_query("select * from clgmaster");

    while($data=mysql_fetch_array($fetchData))
    {
        $id1= $data['ClgId'];
        echo $id1;

        //echo $data['ClgId'];
        echo "<br>";


    }

    //  **** inserting data into course table ****

    $query1="insert into course (ClgId,CourseName) values ($id1,$course)";
    echo $query1;

            $result1 = mysql_query($query1);//Executing course query
            echo $result1;

            if(!$result1)
            {
                die(" <br> Mysql erro".mysql_error());
            }
            else
                echo " <br> record inserted successfully in course";
Posted
Updated 23-Aug-15 2:00am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

Basically there are two options for primary key violaiton, either the record already exists or you call the insert twice (which is basically the same).

Based on your code, it looks like you fetch the value if Id after you have inserted a row into clgmaster. However the select does not restrict any rows so could it be possible that you actually fetch a random row from clgmaster and use that rows id when inserting into course. Since you have just inserted the row into clgmaster, is it necessary to fetch the row at all. Why not use the same value when inserting to course that was used when inserting into clgmaster.
   
Comments
Member 10011989 23-Aug-15 6:51am
   
thanks Mr wendelius ,
i have tried it bt still i m getting same error. i had deleted all the record and execute the code bt still getting same error. i need to insert the same value of clgid into course table . plz suggest some idea. i m confused wht is going wrongwith my code, if u find any plz point me out
Wendelius 23-Aug-15 7:28am
   
While having a closer look it looks like you're executing the insert to clgmaster twice.

Instead of

$result1 = mysql_query($query);//Executing course query

try

$result1 = mysql_query($query1);//Executing course query


Also if the idea is to use common id for clgmaster and course, consider simplifying the code to

$query="insert into clgmaster (ClgId,ClgName,ClgAddrs,FeeAmt,CntNo,Email,Pwd) values ($id,'$clgnm','$addrs',$fees,$cntno,'$eml','$pwd')";

$result = mysql_query($query);//Excecuting clg query

echo $result."<br>";

echo "helllo query <br>";
if ($result)
{
echo "<br>You Have Successfully REGISTERED this Record!";
}

// **** inserting data into course table ****
$query1="insert into course (ClgId,CourseName) values ($id,$course)";
echo $query1;

$result1 = mysql_query($query1);//Executing course query
echo $result1;

if(!$result1)
{
die(" <br> Mysql erro".mysql_error());
}
else {
echo " <br> record inserted successfully in course";
}
Member 10011989 23-Aug-15 8:02am
   
thhanks for pointing my silly mistake near $query1. bt same problem arises after the changes u suggest.
.when i modify my query by inserting manual clgid then i found strang error i.e. Unknown column 'BSc' in 'field list'.where BSc is name of my course name. :(
Wendelius 23-Aug-15 10:24am
   
Should the

$query1="insert into course (ClgId,CourseName) values ($id,$course)";

be

$query1="insert into course (ClgId,CourseName) values ($id,'$course')";
Member 10011989 23-Aug-15 11:09am
   
thanks but i have done it by myself..
thanks for your support..i am very gratefull to you
Wendelius 23-Aug-15 11:12am
   
Could you post the complete code and explain once again what problem you currently have.
Member 10011989 23-Aug-15 11:46am
   
ya for sure. i have posted my solution
Rate this:
Please Sign up or sign in to vote.

Solution 2

I am Trying to insert record in 2 tables which are in primary and foreign key relationship. but i were facing a problem "duplicate primary key" error eventhough the table being totally empty.i need to insert clgId in both the table it will be foreign key for the course table .so here i have solved the problem with using help of expert Mr Mika

here is my complete solution

Quote:
// **** Inserting data into clgmaster table ****

$query="insert into clgmaster (ClgId,ClgName,ClgAddrs,FeeAmt,CntNo,Email,Pwd) values ($id,'$clgnm','$addrs',$fees,$cntno,'$eml','$pwd')";

$result = mysql_query($query);//Excecuting clg query

echo $result."
";

if ($result)
{
echo "
You Have Successfully REGISTERED this Record!";

}
else
die("
mysql error ".mysql_error()); //Checking for Error
echo "
";

// **** inserting data into course table ****

$query1="insert into course (ClgId,CourseName) values ($id,'$course')";
echo $query1;

$result1 = mysql_query($query1);//Executing course query

if(!$result1)
{
die("
Mysql erro".mysql_error());
}
else
echo "
record inserted successfully in course";
}






   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100