zed420 Posted October 19, 2008 Share Posted October 19, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/129097-how-to-populate-the-foreign-key/ Share on other sites More sharing options...
xylex Posted October 19, 2008 Share Posted October 19, 2008 I don't think MyISAM supports fk constraints. Try using InnoDB. Quote Link to comment https://forums.phpfreaks.com/topic/129097-how-to-populate-the-foreign-key/#findComment-669327 Share on other sites More sharing options...
zed420 Posted October 19, 2008 Author Share Posted October 19, 2008 Thanks for reply I've just enabled InnoDB in phpMyAdmin and droped the database recreated db and tables but It didn't work. Thanks anyway Zed Quote Link to comment https://forums.phpfreaks.com/topic/129097-how-to-populate-the-foreign-key/#findComment-669391 Share on other sites More sharing options...
Barand Posted October 19, 2008 Share Posted October 19, 2008 VALUES ('$user_id','$job_id','$cust_name','$cust_address')"; Quote Link to comment https://forums.phpfreaks.com/topic/129097-how-to-populate-the-foreign-key/#findComment-669394 Share on other sites More sharing options...
zed420 Posted October 19, 2008 Author Share Posted October 19, 2008 Thanks Barand its still the same. I need to know how to fetch the user tables primary key value (which is auto incre) to blockbook table's user_id (the foreign key) of this table. thanks zed Quote Link to comment https://forums.phpfreaks.com/topic/129097-how-to-populate-the-foreign-key/#findComment-669410 Share on other sites More sharing options...
Barand Posted October 19, 2008 Share Posted October 19, 2008 After inserting the new user, call $newid = mysql_insert_id(); Must be done before the current mysql connection is closed as this value is per-connection. Quote Link to comment https://forums.phpfreaks.com/topic/129097-how-to-populate-the-foreign-key/#findComment-669413 Share on other sites More sharing options...
zed420 Posted October 19, 2008 Author Share Posted October 19, 2008 Sorry mate fairly new to this where do you want me to place thid code. Quote Link to comment https://forums.phpfreaks.com/topic/129097-how-to-populate-the-foreign-key/#findComment-669594 Share on other sites More sharing options...
Barand Posted October 19, 2008 Share Posted October 19, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/129097-how-to-populate-the-foreign-key/#findComment-669604 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.