Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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