how to manage foreign key, what php coding i need??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bb nicole
    New Member
    • Jan 2007
    • 127

    #1

    how to manage foreign key, what php coding i need??

    I have did the company login and job post which means that company can post the job after they login in my webpage.. I had set company_ID(tabl ename:company) is foreign key of table job. But when i post the job into database, the company_ID show 0(which means when company register, the company_ID should be 2, for example, but in table job, it show 0). What should i do or what php code should i add to get the correct company_ID in table job. And when the company want to post the job(after login), their contact information should be display in the job post form, so they no need to re-enter their information(bec ause database already have their information after they register). But how to do it?? Below is my login function and php code for job post.. Hope someone can help me.. Thanx..

    LOGIN FUNCTION
    [PHP]<?php

    extract ($_POST);

    mysql_connect(" localhost","roo t","")
    or die("Cannot connect to the server");

    mysql_select_db ("ums e-job portal")
    or die("Cannot connect to the database");

    $sql="select username from company where username='$user name'";

    $result=mysql_q uery($sql)
    or die("Cannot execute query");

    $num=mysql_num_ rows($result);


    if($num==1) //username valid
    {
    $sql="select username from company where username='$user name' and password='$pass word'";

    $result2=mysql_ query($sql)
    or die("Cannot execute query");

    $num2=mysql_num _rows($result2) ;
    if($num2>0)
    {
    setcookie("User name", $username, time() + 60 * 60 * 24 * 365);
    $today= date("D F d, h:ia");

    include("welcom e.php");

    }
    else //message send to emp_login.php when wrong password
    {
    $message="The username, $username exists, but you have not entered the correct password! Please try again.<br>";
    include("emp_lo gin.php");
    }
    }
    elseif($num==0) //message send to emp_login.php when username not valid
    {
    $message="The username you entered does not exist! Please try again.<br>";
    include("emp_lo gin.php");
    }



    ?>[/PHP]


    JOB POST FUCTION

    [PHP]<?php
    include_once("d atabase.php");


    class user {


    function linkid ()
    {
    $db = new db();
    $link_id = $db->dbconnect();

    return $link_id;
    } // end function linkid

    function add_user($arr)
    {




    $link_id = $this->linkid();

    $companyName = $arr['companyName'];
    $contactName = $arr['contactName'];
    $emailAdd = $arr['emailAdd'];
    $contactNum = $arr['contactNum'];
    $contactAdd = $arr['contactAdd'];
    $jobTitle = $arr['jobTitle'];
    $jobType = $arr['jobType'];
    $jobCategory = $arr['jobCategory'];
    $jobPosition = $arr['jobPosition'];
    $jobLocation = $arr['jobLocation'];
    $mRequirement = $arr['mRequirement'];
    $jobDescription = $arr['jobDescription '];
    $datePosted = $arr['datePosted'];


    if($companyName ==""){
    $message="Opps. . You forgot enter your Company Name!<br>";
    include("job.ph p");
    }


    else


    if($contactName ==""){
    $message="Opps. . You forgot enter your Contact Name!<br>";
    include("job.ph p");
    }


    else


    if($emailAdd==" "){
    $message="Opps. . You forgot enter your Email Address!<br>";
    include("job.ph p");
    }


    else


    if($contactNum= =""){
    $message="Opps. . You forgot enter your Contact Number!<br>";
    include("job.ph p");
    }


    else


    if($contactAdd= =""){
    $message="Opps. . You forgot enter your Contact Address!<br>";
    include("job.ph p");
    }


    else


    if($jobTitle==" "){
    $message="You need to fill in Title of Your Company's Job!<br>";
    include("job.ph p");
    }


    else


    if($jobType=="" ){
    $message="You need to fill in Type of Your Company's Job!<br>";
    include("job.ph p");
    }


    else



    if($jobCategory ==""){
    $message="You need to fill in Category of Your Company's Job!<br>";
    include("job.ph p");
    }


    else



    if($jobPosition ==""){
    $message="You need to fill in Position of Your Company's Job!<br>";
    include("job.ph p");
    }


    else



    if($jobLocation ==""){
    $message="You need to fill in Location of Your Company's Job!<br>";
    include("job.ph p");
    }


    else



    if($mRequiremen t==""){
    $message="You need to fill in Minimal Requirement of Job!<br>";
    include("job.ph p");
    }


    else

    if($jobDescript ion==""){
    $message="You need to fill in Description of Your Company's Job!<br>";
    include("job.ph p");
    }


    else


    {


    $myquery = "INSERT INTO job ( companyName, contactName, emailAdd, contactNum, contactAdd, jobTitle, jobType, jobCategory, jobPosition, jobLocation, mRequirement, jobDescription, datePosted )".
    "VALUES ( '$companyName', '$contactName' ,'$emailAdd', '$contactNum', '$contactAdd', '$jobTitle', '$jobType', '$jobCategory', '$jobPosition', '$jobLocation', '$mRequirement' , '$jobDescriptio n', now() )";


    $result = mysql_query($my query,$link_id) or die(mysql_error ());

    include_once("j obpost_success. php");


    }
    }
    }


    ?>[/PHP]
  • ctsasikumar
    New Member
    • Nov 2006
    • 17

    #2
    HI ,

    foreign key it's possible innodb table engine only.default mysql table engine is Myisam.so when you create table change the table engine inot innodb

    u create table in through via phpmyadmin..

    select on Table type innodb

    Comment

    • bb nicole
      New Member
      • Jan 2007
      • 127

      #3
      Thanks first.. I don't really understand, can u explain more detail..
      Thanks.. :)

      Comment

      • Motoma
        Recognized Expert Specialist
        • Jan 2007
        • 3236

        #4
        Correct me if I am wrong, but you don't set the company_ID anywhere. If this is the case, then when inserting into the job table, you will get the default value for that field, which appears to be 0.
        You will need to get a value for company_ID somehow. The standard way to do this would be by having the company table have an autoincrement field, and then getting the last inserted id.

        Comment

        Working...