By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,729 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,729 IT Pros & Developers. It's quick & easy.

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

100+
P: 127
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(tablename: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(because 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","root","")
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='$username'";

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

$num=mysql_num_rows($result);


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

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

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

include("welcome.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_login.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_login.php");
}



?>[/PHP]


JOB POST FUCTION

[PHP]<?php
include_once("database.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.php");
}


else


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


else


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


else


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


else


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


else


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


else


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


else



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


else



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


else



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


else



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


else

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


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', '$jobDescription', now() )";


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

include_once("jobpost_success.php");


}
}
}


?>[/PHP]
Jan 27 '07 #1
Share this Question
Share on Google+
3 Replies


P: 17
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
Jan 29 '07 #2

100+
P: 127
Thanks first.. I don't really understand, can u explain more detail..
Thanks.. :)
Jan 31 '07 #3

Motoma
Expert 2.5K+
P: 3,235
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.
Jan 31 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.