Jump to content

php create table


tcorbeil

Recommended Posts

I am able to create a table in mySQL with:

 

mysql_query("CREATE TABLE $Subpage( submitdate DATE NOT NULL, PRIMARY KEY(submitdate), Page TEXT, weblink TEXT, rss TEXT, name VARCHAR(50), email VARCHAR(50))")or die("Create table Error: ".mysql_error());

 

 

but when I enter data into it using:

$query = "INSERT INTO $filename VALUES ('$submitdate','$Page','$weblink','$rss', '$name', '$email')";

 

it seems the table only accepts 1 entry..  so the insert into works once, then doesn't work at all..

 

any ideas?

T.

Link to comment
Share on other sites

first of all, i would reccomend adding {} to your variables

 

$query = "INSERT INTO $filename VALUES ('{$submitdate}','{$Page}','{$weblink}','{$rss}', '{$name}', '{$email}')";

 

Also, I would say you should add a column with an auto increment to avoid conflicts and other problems

 

SpaM!

Link to comment
Share on other sites

your only updating $filename

 

$query = "INSERT INTO $filename VALUES ('$submitdate','$Page','$weblink','$rss', '$name', '$email')";

 

try

$query = "INSERT INTO 'submitdate','Page','weblink','rss', 'name', 'email' VALUES ('$submitdate','$Page','$weblink','$rss', '$name', '$email')";

 

Link to comment
Share on other sites

errr... I thought the $filename was the table...

 

$query = "INSERT INTO 'submitdate','Page','weblink','rss', 'name', 'email' VALUES ('$submitdate','$Page','$weblink','$rss', '$name', '$email')";

 

That wont work cause you arn't specifying a table

Link to comment
Share on other sites

Thanks spamoom.

 

I use the exact script to load into a table I created in PHPadmin.. when i try to upload into that table, it works no problem..

i only doesn't work on tables that have been created by my code in php..

 

There must be something different between the table I created manually and that of the table that is created by code.. I just can't figure it out.. I wish I could see the code that was used by PHPadmin to create the table I created manually...

 

 

Link to comment
Share on other sites

I would like to contradict spam and state the the {} inside the quotes tend to be less efficient. The way you had it is perfectly fine.

 

As for the auto_increment value, that is true, try using this for your table structure:

 

mysql_query("CREATE TABLE $Subpage( submitid int(11) NOT NULL auto_increment, submitdate DATE NOT NULL, Page TEXT, weblink TEXT, rss TEXT, name VARCHAR(50), email VARCHAR(50), PRIMARY KEY(submitid))")or die("Create table Error: ".mysql_error());

 

And change your insert to this:

 

$query = "INSERT INTO $Subpage (`submitdate`, `page`, `weblink`,`rss`,`name`,`email`) VALUES ('$submitdate','$Page','$weblink','$rss', '$name', '$email')";

 

It is generally a good idea to define the columns so you know that the data is going in right.

Link to comment
Share on other sites

You should alwasys set up an auto incrementing id that is tintnt, always, it will make selecting rows faster and avoid all the headaches of typing later.

 

for example:

 

select * FROM tablename WHERE id =  '$id'

 

rather than

 

select * from tablename WHERE blogtext = '$blogtext'

Link to comment
Share on other sites

I do not know about tinyint, as that only goes so far and depending on the size of the database. I generally use Int(11) because that allows for plenty of records and I never had a problem with speed or having to change the int column due to size.

 

But to each their own =)

Link to comment
Share on other sites

You should alwasys set up an auto incrementing id that is tintnt, always, it will make selecting rows faster and avoid all the headaches of typing later.

Fine, if you never need more than 127 records, but when you're dealing with tables containing millions of records then tinyint isn't going to cut it.

Link to comment
Share on other sites

I have always used tinyint simply for that reason, I generally on have 50-60 records, however I do have some with 30,000 records running int. I suppose I should have been more specific. But I am glad to see it is working.

Link to comment
Share on other sites

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.