Jump to content

Recommended Posts

Hi All

Can some please enlighten me how to populate the foreign key in php. I have two tables;

CREATE TABLE `user` (
  `id` smallint(5) unsigned NOT NULL auto_increment,
  `fullname` varchar(60) NOT NULL,
  `username` char(15) NOT NULL,
  `pswd` char(32) NOT NULL,
  `address` varchar(70) NOT NULL,
    PRIMARY KEY  (`id`)
  ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE `blockBook` (
  `user_id` smallint(5) unsigned NOT NULL,
  `job_id` smallint(5) unsigned NOT NULL auto_increment,
  `cust_name` varchar(40) NOT NULL,
  `cust_address` varchar(60) NOT NULL,
    PRIMARY KEY  (`job_id`),
  FOREIGN KEY (user_id) REFERENCES user(id) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

 

This is how I'm INSERTING into the table;

$user_id = $_POST['user_id'];
$job_id = $_POST['job_id'];
$cust_name = $_POST['cust_name'];
$cust_address = $_POST['cust_address'];

$query = "INSERT INTO blockbook  VALUES
('user_id','job_id','$cust_name','$cust_address')";

   $result = mysql_query($query)or die(mysql_error());
}

And this is my SELECT query;

 

$query = "SELECT  * FROM blockbook, user  
WHERE  user_id= id";
   $result = mysql_query($query)or die(mysql_error());

But it’s not working the foreign key value is always 0.

How the devil do I populate it? I've even tried manually it doesn't work.

Thanks

Zed

 

Link to comment
https://forums.phpfreaks.com/topic/129097-how-to-populate-the-foreign-key/
Share on other sites

Well, as it needs to be called after you insert the new user record and you need the value to place in the inserted new blockbook record, I would suggest somewhere between the two inserts. The normal place is immediately after the first insert.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.