views:

54

answers:

5

I have a file named discussion.php, where a form is located. In this form, a user will enter information, and the information will be posted to savedisc.php. Below is the code for discussion.php:

<form action='savedisc.php' method='post'>
  <p>What would you like to discuss?</p>
  <textarea name='message' rows='15' cols='40'></textarea>
  <input type='submit' value='Submit' />


</form>

Once the user hits the submit button, the text that the user had typed should be saved via savedisc.php. I.e. I will connect to the database, and save what the user had typed in textarea.

Now to display the information that the user had typed in the textarea, I will connect to the database in another file, and show the appropriate content.

Some of my questions are: Should I not save it in the database? Because later on I might have enormous amounts of data in my tables. Is there another way to display information submitted by a user without the use of saving to the database? Or am I doing OKAY? For example, the question I am about to post right now, is it actually saved in a database?

Thank you.

+3  A: 

MySQL is perfectly capable to handle tables with several gigabytes of data. I doubt this will ever be a problem.

The alternative being to store the data in flat files, it's clearly better to use MySQL for this purpose.

Artefacto
+4  A: 

Yes! Databases are good at storing large quantities of data, so you're fine!

As long as you do proper validation of data*, go right ahead!

* - Look up mysql_real_escape_string

Mike Caron
Validation is not just limited to escaping for database storage. Beware if your user enters (HTML, Javascript, CSS references...) which may disrupt or break something when it is sent back to someone else's browser. As far as the browser (and user) is concerned, everything you are sending is coming from YOUR website. Doesn't matter if it was originally entered by another user.
Gary
Yes, proper validation is important. However, it's slightly off topic here. I only mentioned it because... well, I'm not sure why.
Mike Caron
+1  A: 

yes, that's an entirely appropriate use of a database. An exception might be if If the size of each piece of text is very large, like 10 or 50 or 150kb, in which case you might consider storing it as files and storing file metadata in the database.

Alex JL
Even then, there are datatypes that could do much of that work for you (CLOBs / BLOBs).
Craig Trader
@W. True, it's just that I personally don't know why one would want to store large binary objects directly in a database, when that is what file systems were made for, after all. I'm sure people have their reasons (I wouldn't mind finding out).
Alex JL
@Alex, Security is one reason for storing BLOBs in a database. It makes it harder to access them except through the application. Another is if you are using text indexing features in the database.
Gary
@Gary, I see, thanks. I guess DB engines are like specialized file systems, after all.
Alex JL
it seems like you could just be using your OS's ACL/permissions system to deny or permit access, though. If you can't rely on that - can't the users potentially read where your database tables are stored in the file system, then, bypassing the DB layer and your application?
Alex JL
+1  A: 

Databases are meant to store large amount of data and handle large amount of transactions. So, definitely yes.

But if you're thinking of optimizing your database and save some resources, you can store the data the user just entered and show it without requesting it again from the database, like when you ask a question and it shows it to you again on Stack Overflow, you'd have one less transaction.

So, for example in your savedisc.php

<?php
$message = $_POST['message'];
StoreInDB($message); // some function to store data
echo $message; // this is useful when you want to immediately
               // show the user the information they entered
?>
Hamid Nazari
+1  A: 

Most CMSes use a database backend to store the actual text and markup for their stories; large sites such as the New York Times and the Washington Post use databases to store their stories, so that they can be searched and cross-referenced as necessary. While it is possible to do all of that as files in a filesystem, modern databases are optimized for these sorts of operations.

One thing you'll need to do is to choose the type of table and datatype to use for storing your data. MySQL provides several storage engines for creating tables (InnoDB and MyISAM are the most popular). As for data types, CHAR and VARCHAR are good for relatively small amounts of text, with TEXT being suitable for large amounts of text. (If you're storing raw binary data, like images, instead of text, then you'll be more interested in BINARY, VARBINARY, and BLOB).

Each storage engine and datatype will provide different trade-offs for performance and functionality, but for relatively small and simple applications, MyISAM and either VARCHAR or TEXT will be a good choice for your needs.

Craig Trader