Go Back   nV News Forums > Software Forums > Software Development

Newegg Daily Deals

Reply
 
Thread Tools
Old 07-06-07, 10:00 AM   #1
ViN86
 
Join Date: Mar 2004
Posts: 15,486
Default Storing Text Area into MySQL Database...

i created a <TEXTAREA> to allow users to enter comments. i want to store this in the database. my questions are:

1. what type of field should it be? varchar(MAX)?
2. what is the maximum length of the column in the database? i want to limit the text area so i can prevent users from entering too much text
3. how do i store the text from the text area into the database? what value do i reference? is it just like a normal text type input?

heres what my code looks like for the textarea:
Code:
<tr>
	       <td colspan="2" align="center"><TEXTAREA NAME="Comments"       
                 ROWS="8" COLS="100"  
                 style="color:White;background-color:Black;width:600px;">
                 </TEXTAREA>
          </tr>
ViN86 is offline   Reply With Quote
Old 07-06-07, 10:40 AM   #2
fivefeet8
Ngemu Mod
 
Join Date: Jul 2003
Location: Fresno, CA
Posts: 1,886
Default Re: Storing Text Area into MySQL Database...

If you want to limit text, you should use a char type field. This will allow you to limit the characters inputted to a maximum of 255 characters. Text fields in mysql are limited to 65kb. There are other larger type text fields in mysql as well. Of course you could also use php or javascript to check and keep the posted string at a certain size before adding them into the table.
__________________
[i7 2600k @4.4ghertz][2x4 GB DDR3 1600][EVGA GTX570 1.280GB SC][EVGA GTX460 physx][Asrock Extreme7 Gen3 Z68][2xSeagate 160 Gb SATA HD raid0][Seagate 250 GB SATA2 HD][Sony Bravia 40' 1080p LCD HDTV][NEC 3520a DVD+-DLw][Windows 7 Ultimate x64][Rosewill 1000w]
fivefeet8 is offline   Reply With Quote
Old 07-06-07, 11:28 AM   #3
evilghost
Registered User
 
Join Date: Jul 2005
Posts: 3,606
Default Re: Storing Text Area into MySQL Database...

1) I use TEXT (65535 characters). Other choices can be BLOB (binary). In your case TEXT would be better than CHAR or VARCHAR. See http://dev.mysql.com/doc/refman/5.0/...uirements.html

2) TEXT is 65535, or 2^16

3) $_POST['Comments'] but you better be sure you substr(mysql_escape_string($_POST['Comments']),0,65535)
evilghost is offline   Reply With Quote
Old 07-06-07, 01:03 PM   #4
ViN86
 
Join Date: Mar 2004
Posts: 15,486
Default Re: Storing Text Area into MySQL Database...

Quote:
Originally Posted by evilghost
1) I use TEXT (65535 characters). Other choices can be BLOB (binary). In your case TEXT would be better than CHAR or VARCHAR. See http://dev.mysql.com/doc/refman/5.0/...uirements.html

2) TEXT is 65535, or 2^16

3) $_POST['Comments'] but you better be sure you substr(mysql_escape_string($_POST['Comments']),0,65535)
thx ghost didnt know about that 3rd one.
ViN86 is offline   Reply With Quote
Old 07-06-07, 01:04 PM   #5
evilghost
Registered User
 
Join Date: Jul 2005
Posts: 3,606
Default Re: Storing Text Area into MySQL Database...

Glad to help, just make sure you're running mysql_escape_string() on any user-passed data you're writing to the database to avoid SQL injection.
evilghost is offline   Reply With Quote
Old 07-06-07, 07:50 PM   #6
radekhulan
 
radekhulan's Avatar
 
Join Date: Apr 2005
Location: Prague
Posts: 749
Default Re: Storing Text Area into MySQL Database...

Better use mysqli_real_escape_string(), it takes opened DB connection into account, and escapes string according to its charset (UTF8 by default).
__________________
[size=1][b]CPU:[/b] Core 2 Quad Q6600 @ 3.2GHz | [b]Memory:[/b] 4x2GB Patriot DDR2-800 | 4GB A-Data PD7 ReadyBoost USB | [b]Sound:[/b] X-Fi FPS
[b]Board:[/b] Asus Commando | [b]GPU: 8800GTX @ 650/1050[/b] | [b]HDD:[/b] 3x WD3200KS 320GB + 500GB | [b]Power:[/b] Fortron EPSILON 600W-GLN
[b]Mouse:[/b] Logitech G5 | [b]Kbd:[/b] Logitech G15 | [b]DVD:[/b] Plextor 130B + LITE-ON LH-18A1H | [b]LCD[/b] [b]24" HP2465 1920x1200[/b] | Vista Ultimate x64
My websites: [url=http://hulan.cz/en][b]HULAN.cz[/b][/url], [url=http://radekhulan.cz/][b]RADEKHULAN.cz[/b][/url], [url=http://hulan.info/][b]HULAN.info[/b][/url], [url=http://blogcms.com/][b]BLOGCMS.com[/b][/url], [url=http://www.megablog.cz/][b]MEGABLOG.cz[/b][/url] | [url=http://hulan.cz/temp/games.png][b]Games played[/b][/url] | [URL=http://hulan.cz/temp/sestava.png][B]CPU-Z[/B][/URL] -- [URL="http://radekhulan.cz/img/stacker-830/stacker-830-bocnice.jpg"][B]case foto[/B][/URL][/size]
radekhulan is offline   Reply With Quote
Old 07-06-07, 09:04 PM   #7
evilghost
Registered User
 
Join Date: Jul 2005
Posts: 3,606
Default Re: Storing Text Area into MySQL Database...

True, evidently mysql_escape_string() is now depreciated; no idea when they ddi that.
evilghost is offline   Reply With Quote
Old 07-08-07, 04:09 AM   #8
t3hl33td4rg0n
Dethklok Returns!
 
t3hl33td4rg0n's Avatar
 
Join Date: Dec 2004
Location: Kent, OH
Posts: 2,142
Send a message via AIM to t3hl33td4rg0n Send a message via MSN to t3hl33td4rg0n Send a message via Yahoo to t3hl33td4rg0n
Default Re: Storing Text Area into MySQL Database...

Probably because they want to use mysqli_xxx for functions of that sort instead of mysql_xxx. Just a guess though.
__________________


Intel QX9650 @ 3.83GHzXFX 780ieVGA GTX 560Ti8GB GSkill DDR800Creative Sound Blaster X-Fi Titanium PCI-ESeagate 1.5TB . 4x Samsung 2TB

Quote:
Originally Posted by MowTin View Post
....it's so clear how government makes things worse not better...
t3hl33td4rg0n is offline   Reply With Quote

Old 07-08-07, 03:18 PM   #9
radekhulan
 
radekhulan's Avatar
 
Join Date: Apr 2005
Location: Prague
Posts: 749
Default Re: Storing Text Area into MySQL Database...

In fact, there are 3 ways to manipulate MySQL DB.

* mysql set of functions (MySQL 3.x, 4.0) - mysql_escape_string()
* mysqli set of functions (MySQL 4.1, 5.x) - mysqli_real_escape_string()
* PDO set of functions (can be used as a DB layer) - PDO::quote()

If you are coding a new project, you should use PDO, as it is database independent (if you do it right way):
http://php.net/pdo
__________________
[size=1][b]CPU:[/b] Core 2 Quad Q6600 @ 3.2GHz | [b]Memory:[/b] 4x2GB Patriot DDR2-800 | 4GB A-Data PD7 ReadyBoost USB | [b]Sound:[/b] X-Fi FPS
[b]Board:[/b] Asus Commando | [b]GPU: 8800GTX @ 650/1050[/b] | [b]HDD:[/b] 3x WD3200KS 320GB + 500GB | [b]Power:[/b] Fortron EPSILON 600W-GLN
[b]Mouse:[/b] Logitech G5 | [b]Kbd:[/b] Logitech G15 | [b]DVD:[/b] Plextor 130B + LITE-ON LH-18A1H | [b]LCD[/b] [b]24" HP2465 1920x1200[/b] | Vista Ultimate x64
My websites: [url=http://hulan.cz/en][b]HULAN.cz[/b][/url], [url=http://radekhulan.cz/][b]RADEKHULAN.cz[/b][/url], [url=http://hulan.info/][b]HULAN.info[/b][/url], [url=http://blogcms.com/][b]BLOGCMS.com[/b][/url], [url=http://www.megablog.cz/][b]MEGABLOG.cz[/b][/url] | [url=http://hulan.cz/temp/games.png][b]Games played[/b][/url] | [URL=http://hulan.cz/temp/sestava.png][B]CPU-Z[/B][/URL] -- [URL="http://radekhulan.cz/img/stacker-830/stacker-830-bocnice.jpg"][B]case foto[/B][/URL][/size]
radekhulan is offline   Reply With Quote
Old 07-08-07, 05:58 PM   #10
evilghost
Registered User
 
Join Date: Jul 2005
Posts: 3,606
Default Re: Storing Text Area into MySQL Database...

I've never heard of PDO, excellent information, thanks radekhulan.
evilghost is offline   Reply With Quote
Old 07-09-07, 05:30 AM   #11
radekhulan
 
radekhulan's Avatar
 
Join Date: Apr 2005
Location: Prague
Posts: 749
Default Re: Storing Text Area into MySQL Database...

In fact, PDO is extremely important for SQLite, as there is no way to access SQLITE3 except PDO. sqlite() functions support SQLITE2 only, which is buggy and limited in functionality. PDO functions can support SQLITE2 *and* SQLITE3:
http://www.php.net/manual/en/ref.pdo.php#pdo.drivers
__________________
[size=1][b]CPU:[/b] Core 2 Quad Q6600 @ 3.2GHz | [b]Memory:[/b] 4x2GB Patriot DDR2-800 | 4GB A-Data PD7 ReadyBoost USB | [b]Sound:[/b] X-Fi FPS
[b]Board:[/b] Asus Commando | [b]GPU: 8800GTX @ 650/1050[/b] | [b]HDD:[/b] 3x WD3200KS 320GB + 500GB | [b]Power:[/b] Fortron EPSILON 600W-GLN
[b]Mouse:[/b] Logitech G5 | [b]Kbd:[/b] Logitech G15 | [b]DVD:[/b] Plextor 130B + LITE-ON LH-18A1H | [b]LCD[/b] [b]24" HP2465 1920x1200[/b] | Vista Ultimate x64
My websites: [url=http://hulan.cz/en][b]HULAN.cz[/b][/url], [url=http://radekhulan.cz/][b]RADEKHULAN.cz[/b][/url], [url=http://hulan.info/][b]HULAN.info[/b][/url], [url=http://blogcms.com/][b]BLOGCMS.com[/b][/url], [url=http://www.megablog.cz/][b]MEGABLOG.cz[/b][/url] | [url=http://hulan.cz/temp/games.png][b]Games played[/b][/url] | [URL=http://hulan.cz/temp/sestava.png][B]CPU-Z[/B][/URL] -- [URL="http://radekhulan.cz/img/stacker-830/stacker-830-bocnice.jpg"][B]case foto[/B][/URL][/size]
radekhulan is offline   Reply With Quote
Old 07-09-07, 08:00 AM   #12
ViN86
 
Join Date: Mar 2004
Posts: 15,486
Default Re: Storing Text Area into MySQL Database...

hmmm, PDO. ill have to look into that, thx


ill use the mysqli_xxx cmds for the escape strings too, thx.
ViN86 is offline   Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 12:18 PM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.