Jump to content


Photo

Help creating tables??


  • Please log in to reply
2 replies to this topic

#1 ryan.od

ryan.od
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 24 September 2006 - 08:34 PM

Can anyone tell me what is wrong with this? The reviews_post table and the reviews_post_category table generate an error and I don't know why.

Also, the post -> category relationship is many-to-many. As such, I created the reviews_post_category table that only has foreign keys in it that reference the post ID and the category ID in their respective tables. Is this the correct approach?

Thanks,
Ryan

<?php

require_once('db_connect.php');

mysql_select_db ('evaluati_reviews');

$sql = 'CREATE TABLE reviews_author
(
  id_author int(11) NOT NULL auto_increment,
  fname_author varchar(30) NOT NULL,
  lname_author varchar(50) NOT NULL,
 
  PRIMARY KEY (id_author),
)';

$result = mysql_query($sql) or print ("Can't create the table 'reviews_author' in the database.<br />" . $sql . "<br />" . mysql_error());

if ($result != false) {
echo('Article database created successfully!');
}

$sql = 'CREATE TABLE reviews_date
(
  id_date int(11) NOT NULL auto_increment,
  date_date timestamp(8) NOT NULL,
 
  PRIMARY KEY (id_date),
)';

$result = mysql_query($sql) or print ("Can't create the table 'reviews_date' in the database.<br />" . $sql . "<br />" . mysql_error());

if ($result != false) {
echo('Article database created successfully!');
}

$sql = 'CREATE TABLE reviews_category
(
  id_category int(11) NOT NULL auto_increment,
  cat_category varchar(40) NOT NULL,
 
  PRIMARY KEY (id_category),
)';

$result = mysql_query($sql) or print ("Can't create the table 'reviews_category' in the database.<br />" . $sql . "<br />" . mysql_error());

if ($result != false) {
echo('Article database created successfully!');
}

$sql = 'CREATE TABLE reviews_post
(
  id_post int(11) NOT NULL auto_increment,
  idpost_date int(11),
  idpost_author int(11),
  title_post varchar(100) NOT NULL,
  intro_post varchar(200) NOT NULL,
  text_post text NOT NULL,

  PRIMARY KEY (id_post),
  FOREIGN KEY idpost_date REFERENCES reviews_date(id_date),
  FOREIGN KEY idpost_author REFERENCES reviews_author(id_author)
)';

$result = mysql_query($sql) or print ("Can't create the table 'reviews_post' in the database.<br />" . $sql . "<br />" . mysql_error());

if ($result != false) {
echo('Article database created successfully!');
}

$sql = 'CREATE TABLE reviews_post_category
(
  id_post int(11),
  id_category int(11),

  PRIMARY KEY (id_post, id_category),
  FOREIGN KEY id_post REFERENCES reviews_post(id_post),
  FOREIGN KEY id_category REFERENCES reviews_category(id_category),
)';

$result = mysql_query($sql) or print ("Can't create the table 'reviews_post_category' in the database.<br />" . $sql . "<br />" . mysql_error());

if ($result != false) {
echo('Article database created successfully!');
}

$sql = 'CREATE TABLE reviews_link
(
  id_link int(11) NOT NULL auto_increment,
  url_link varchar(150) NOT NULL,
 
  PRIMARY KEY (id_link),
)';

$result = mysql_query($sql) or print ("Can't create the table 'reviews_link' in the database.<br />" . $sql . "<br />" . mysql_error());

if ($result != false) {
echo('Article database created successfully!');
}

?>

#2 ryan.od

ryan.od
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 24 September 2006 - 11:02 PM

Nevermind. The problem was that I didn't enclose my FOREIGN KEYS in paranthesis. Duh.

#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 25 September 2006 - 03:54 AM

Plus, AFAIK, you can't have a trailing comma.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users