FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Can't create new table.





engammalsko
Hi! I tried to create a table with this code...

Code:
mysql_query("CREATE TABLE test (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 40 ) NOT NULL ,
`comment` VARCHAR( 250 ) NOT NULL
) ENGINE = MYISAM ;", $connect);


I have connected and choosed right database, I can post data to an existing table too!
But I can't create one.

What could been wrong? I use the $connect variable to post data to an existing table, so that shouldn't be the problem.

And I don't get any error messages either. There is just nothing that's happening D:
Please help.
badai
did you give that db user privilege to create table?

assuming you are on frihost, go to your control panel, select mysql management, select the database, then click modify privileges. you can see what the user can and cannot do.
fladry
You can create tables directly from phpmyadmin and export them anywhere you need. It is lot easier.
davidv
From my very limited knowledge on using mySQL (like less than 3 days). I've altered what you typed slightly, threw it into the cmd shell and it was fine.

Code:
mysql> CREATE TABLE test (
    -> id SMALLINT UNSIGNED AUTO_INCREMENT,
    -> name VARCHAR(40),
    -> comment VARCHAR(250) NOT NULL,
    -> PRIMARY KEY (id)
    -> ) ENGINE = InnoDB;
Query OK, 0 rows affected (0.12 sec)
engammalsko
Sorry, guys... I spend hours of figureing it out... And the problem was just javascript -.-'

fladry: sometimes you wanna create a table when for example somone clicks a button on a page.
fladry
engammalsko wrote:
Sorry, guys... I spend hours of figureing it out... And the problem was just javascript -.-'

fladry: sometimes you wanna create a table when for example somone clicks a button on a page.

Yeah sure but i dont think you need to create a dynamic table for each click (in most cases, you dont even need to create a table when someone clicks a button). And phpmyadmin gives you the code at the end when you save the table. So, you can copy the code and paste to make the table at any time.

Anyway, learning to create the tables the hard way is also good.
ankur209
haha.... Razz javascript ??? how ?
speeDemon
@fladry
Well, let's say I want to make a page which allows users to comment on photos. I'm not really even an intermediate, but I'm pretty sure that when the person presses "post" or the like, then the code should check if there is an existing table, and if not, then it would create one, and then perform the rest of the activity. You can't go on making new tables for people's comments on your own, especially on websites like facebook, where each user has hundreds of photo uploads.
fladry
speeDemon wrote:
@fladry
Well, let's say I want to make a page which allows users to comment on photos. I'm not really even an intermediate, but I'm pretty sure that when the person presses "post" or the like, then the code should check if there is an existing table, and if not, then it would create one, and then perform the rest of the activity. You can't go on making new tables for people's comments on your own, especially on websites like facebook, where each user has hundreds of photo uploads.


A site like facebook would surely require that but i dont think user commenting system needs creation of a seperate table for each comment(or for each photo or album) if the database is neatly designed and i dont think you'd be creating facebook at this level Smile. That way would create far too many tables in your database. You can store all the comments in a single table with each comment indexed to the id of the photo or something like that. I have created many websites but none of them have required creation of tables at runtime.
Create a table photos where each photo has an ID
create a table comment with three fields- comment text, photo id, and commenting date. When a user comments in a photo, simply add a comment with the id of that photo and current date(and time).

Even if you require the table, create the table in phpmyadmin, copy the code and simply change the table name when you require to create another similar table.
engammalsko
Well, I have made a blog http://engammalsko.co.cc.
And atm there is separate blogpost but it's the same comments on all the blogposts.

So my idea was to insert a table everytime I post a blogposts and make that table connected to the blogposts so just the right comments shows at the right blogpost Razz
Fire Boar
Yeah, that's the wrong way to do it. The right way is to use table relations. Here's an example table layout.

Code:
CREATE TABLE blogpost (
  id INT(11) UNSIGNED AUTO_INCREMENT,
  title VARCHAR(255),
  body TEXT,
  PRIMARY KEY(id)
) ENGINE = InnoDB;

CREATE TABLE comment (
  id INT(11) UNSIGNED AUTO_INCREMENT,
  blogpost_id INT(11) UNSIGNED,
  title VARCHAR(255),
  body TEXT,
  PRIMARY KEY(id),
  CONSTRAINT comment_fk FOREIGN KEY(blogpost_id) REFERENCES blogpost(id) ON DELETE CASCADE
) ENGINE = InnoDB;


Now you store all your comments in one table, and when you come to select the comments, you would filter it by blogpost_id. So for example, when viewing blogpost 24,

Code:
SELECT title, body FROM comment WHERE blogpost_id=24;


One thing to note about the table schema above is this part:

Code:
CONSTRAINT comment_fk FOREIGN KEY(blogpost_id) REFERENCES blogpost(id) ON DELETE CASCADE


This means, create a foreign key relation named comment_fk (it doesn't matter what you call it), linking to the id column in the blogpost table. ON DELETE CASCADE means if an entry in the blogpost table is deleted, all comments for that entry (i.e. all comments with a blogpost_id that matches the deleted blogpost's id) are also deleted. There are other ON DELETE options too. For example, ON DELETE SET NULL is useful for if you also have a table of users and link users to comments: if a user is deleted then all their comments are not deleted but attributed to a guest user.
speeDemon
fladry wrote:
A site like facebook would surely require that but i dont think user commenting system needs creation of a seperate table for each comment(or for each photo or album) if the database is neatly designed and i dont think you'd be creating facebook at this level Smile.

Create a table photos where each photo has an ID
create a table comment with three fields- comment text, photo id, and commenting date. When a user comments in a photo, simply add a comment with the id of that photo and current date(and time)..

Thanks for the information! and ya, I can't possibly even think of creating something that big with the current amount of knowledge I have Smile
Lucifer_iix
For developing a database i can recommend you:

MySQL Workbench 5.2 CE

You can make nice diagrams, but also triggers and procedures...



http://dev.mysql.com/downloads/workbench/


http://wb.mysql.com/?page_id=10
Related topics
E-Cards with PHP & MySQL
Build an online dictionary by PHP/MySQL
SLOW UPLOADING AN SQL FILE
I cant install php chat tables nor set up diffrent subdomain
Tutorial: PHP Installed Modules Dynamic Reference Tool
Forum Problem
Can't create mySQL table
mySQL create table problem (parse error)..
Auto create tables
Creating a new phpbb forum on my new domain.
Can΄t create new file in CPanel
Can't create a new database [resolved]
a new mysql table...
unable to create new domain in direct admin
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.