Jump to content

insert into sql table that has primary key of 2 values


android6011

Recommended Posts

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

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')";"

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?

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.