Jump to content

how to populate the foreign key


zed420

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.