android6011 Posted April 28, 2008 Share Posted April 28, 2008 I made a table like $sql4="CREATE TABLE comments(commentid INT NOT NULL AUTO_INCREMENT, articleid INT NOT NULL, PRIMARY KEY(commentid,articleid), memberid INT, datepost VARCHAR(35), comments MEDIUMTEXT, Foreign Key (memberid) references members(memberid),Foreign Key (articleid) references articles(articleid));"; so now I need some help inserting a comment into it. I'm not sure how to since it has a dual primary key Link to comment https://forums.phpfreaks.com/topic/103339-insert-into-sql-table-that-has-primary-key-of-2-values/ Share on other sites More sharing options...
DarkWater Posted April 28, 2008 Share Posted April 28, 2008 Why would you make 2 primary keys? Primary on dictionary.com Link to comment https://forums.phpfreaks.com/topic/103339-insert-into-sql-table-that-has-primary-key-of-2-values/#findComment-529228 Share on other sites More sharing options...
android6011 Posted April 28, 2008 Author Share Posted April 28, 2008 i dont mean 2 primary keys, i mean the primary key has 2 references, i was told to do it in another thread a while back since it referes to another table Link to comment https://forums.phpfreaks.com/topic/103339-insert-into-sql-table-that-has-primary-key-of-2-values/#findComment-529233 Share on other sites More sharing options...
DarkWater Posted April 28, 2008 Share Posted April 28, 2008 Show us the articles table scheme too. SHOW CREATE TABLE articles; Post the output of that. Link to comment https://forums.phpfreaks.com/topic/103339-insert-into-sql-table-that-has-primary-key-of-2-values/#findComment-529238 Share on other sites More sharing options...
Barand Posted April 28, 2008 Share Posted April 28, 2008 Change to <?php $sql4="CREATE TABLE comments( commentid INT NOT NULL AUTO_INCREMENT, articleid INT NOT NULL, memberid INT, datepost VARCHAR(35), comments MEDIUMTEXT, PRIMARY KEY(articleid, commentid), // <<< change to this Foreign Key (memberid) references members(memberid), Foreign Key (articleid) references articles(articleid) )"; ?> and insert with $sql2 = "INSERT INTO comments (articleid, memberid, datepost, comments) VALUES ($articleid, $memberid, CURDATE(), '$comments')";" Link to comment https://forums.phpfreaks.com/topic/103339-insert-into-sql-table-that-has-primary-key-of-2-values/#findComment-529240 Share on other sites More sharing options...
android6011 Posted April 29, 2008 Author Share Posted April 29, 2008 CREATE TABLE `articles` ( `articleid` int(11) NOT NULL auto_increment, `memberid` int(11) default NULL, `datepost` varchar(35) default NULL, `article` mediumtext, `articletitle` varchar(60) default NULL, PRIMARY KEY (`articleid`), KEY `memberid` (`memberid`) ) CREATE TABLE `comments` ( `commentid` int(11) NOT NULL auto_increment, `articleid` int(11) NOT NULL, `memberid` int(11) default NULL, `datepost` varchar(35) default NULL, `comment` mediumtext, PRIMARY KEY (`commentid`,`articleid`), KEY `memberid` (`memberid`), KEY `articleid` (`articleid`) ) is it normal for show create tables to just put "key" not foreign key? Link to comment https://forums.phpfreaks.com/topic/103339-insert-into-sql-table-that-has-primary-key-of-2-values/#findComment-529250 Share on other sites More sharing options...
android6011 Posted April 29, 2008 Author Share Posted April 29, 2008 I dont have anything in the comments table at the moment so i dont mind recreating that, but if possible i would like to leave the articles table the way it is, unless it would be better off a different way Link to comment https://forums.phpfreaks.com/topic/103339-insert-into-sql-table-that-has-primary-key-of-2-values/#findComment-529252 Share on other sites More sharing options...
android6011 Posted April 29, 2008 Author Share Posted April 29, 2008 also, now how would i get comments for a certain article id? thanks Link to comment https://forums.phpfreaks.com/topic/103339-insert-into-sql-table-that-has-primary-key-of-2-values/#findComment-529265 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.