ok im makeing a simple database for my example, there is users data and the user company's data .
CREATE TABLE `users` (
`UID` INT(25) NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(60) NOT NULL ,
`password` VARCHAR(100) NOT NULL ,
`ownername` VARCHAR(150) NOT NULL ,
`userstatus` TINYINT(1) NOT NULL ,
`userregistertime` DATETIME NOT NULL ,
`userlastonline` DATETIME NOT NULL ,
PRIMARY KEY (`UID`) ,
INDEX `username` (`username` ASC) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
CREATE TABLE `company` (
`CID` INT(25) NOT NULL AUTO_INCREMENT ,
`UID` INT(25) NOT NULL ,
`companyname` VARCHAR(60) NOT NULL ,
`companyaddress` VARCHAR(255) NOT NULL ,
`companyemail` VARCHAR(255) NULL DEFAULT NULL ,
`companyphone` VARCHAR(20) NOT NULL ,
`companyimage` VARCHAR(255) NULL DEFAULT NULL ,
`companyyahoo` VARCHAR(255) NULL DEFAULT NULL ,
`companytwitter` VARCHAR(255) NULL DEFAULT NULL ,
`companykaskus` VARCHAR(255) NULL DEFAULT NULL ,
`companyfacebook` VARCHAR(255) NULL DEFAULT NULL ,
`companytype` TINYINT(1) NOT NULL DEFAULT '0' ,
`companystatus` TEXT NULL DEFAULT NULL ,
`companytemplate` TEXT NULL DEFAULT NULL ,
`companyintroduction` TEXT NULL DEFAULT NULL ,
`partnership` TINYINT(1) NOT NULL DEFAULT '0' ,
PRIMARY KEY (`CID`) ,
INDEX `ownername` (`UID` ASC) ,
INDEX `companyname` (`companyname` ASC) ,
CONSTRAINT `ownernamecompany`
FOREIGN KEY (`UID` )
REFERENCES `users` (`UID` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
1.why after i insert data to the users table ( uid is auto increment ) it doesnt update my company UID table ?
if you have php knowledge please see how im inserting it.
$RegisterInsert1 = $dbConnect->prepare("INSERT INTO users (
`username`, `password`, `ownername`, `userregistertime`, `userlastonline`) VALUES (
:username, :password, :ownername, :userregistertime, :userlastonline)");
$RegisterInsert1->execute($RegisterData1);
as you see, i get, UID = 1 ( auto ) then username,pasword,ownername,etc inserted on user table. but somehow my company UID is not updated. it should be UID = 1 then the rest CID ( auto ) companyname null, etc null.
2.am i right defining a foreign key usage for?
3.please give me the best example of inserting users data + company data with the right usage from the right usage of foreign.
how im doing it right now
// INSERT USERS DB
$RegisterInsert1 = $dbConnect->prepare("INSERT INTO users (
`username`, `password`, `ownername`, `userregistertime`, `userlastonline`) VALUES (
:username, :password, :ownername, :userregistertime, :userlastonline)");
$RegisterInsert1->execute($RegisterData1);
// GET USERS GIVEN AUTO GENERATED UID
// QUESTION ? THIS one should be automated by foreign useage ?
$GetUid = $dbConnect->prepare("SELECT UID FROM users WHERE username = :username");
$GetUid->execute($RegisterData3);
$UserID = $GetUid->fetch();
$RegisterData2['UID'] = $UserID;
// INSERT COMPANY INFO + UID
$RegisterInsert2 = $dbConnect->prepare("INSERT INTO company (
`UID`,`companyphone`,`partnership`) VALUES (
:UID, :companyphone, :partnership)");
$RegisterInsert2->execute($RegisterData2);