Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more

Go Back   Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more > Web Programming > PHP Development
User Name
Password

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-16-2006, 01:35 PM   #1 (permalink)
jenz
Junior Member
 
Join Date: Apr 2006
Posts: 1
Exclamation textarea to mysql

How can i send the content of a textarea to my mysql db?
i've noticed that it won't send the <br>'s and stuff with it...

Can someone post an example how to do it? The full code if it is possible...

greetz
jenz is offline   Reply With Quote
Sponsored Links
Old 05-06-2006, 04:46 PM   #2 (permalink)
etono
Moderator
 
Join Date: May 2006
Posts: 86
Default Re: textarea to mysql

Here is how I would do it. Maybe someone else has a better method.
Just for refrence this is the db table I set up:

Code:
CREATE TABLE `comments` ( `comment_id` smallint(6) NOT NULL auto_increment, `comment_text` text NOT NULL, PRIMARY KEY (`comment_id`) );

and my html page:
Code:
<html> <head> <title>Post a comment</title> </head> <body> <form action="comment.php" method="post"> <textarea name="comment"></textarea> <input type="submit" value="Submit Comment" /> </form> </body> </html>

now the php handling file (comment.php):
Code:
<?php //assuming a connection to the database exists $mysql = array(); // create array of mysql escaped values $mysql['comment'] = mysql_real_escape_string( $_POST['comment'] ); $sql = "INSERT INTO comments ( comment_text ) VALUES ( '{$mysql['comment']}' )"; //insert your comment to db mysql_query( $sql ); ?>

This is saving the line breaks and escaping your quotes. The line breaks are being saved as '\n' the newline, so you have to convert these into <br> html line breaks. Also you will want to strip out the slashes from the quotes. So to display your comments do something like (comments.php):
Code:
<?php //assuming a connection to the database exists $sql = "SELECT * FROM comments"; $result = mysql_query( $sql ); while( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) ) { $html = array(); //create an array of html formated values. $html['comment'] = nl2br( stripslashes( htmlentities( $row['comment_text'], ENT_QUOTES, 'UTF-8' ) ) ); echo "Comment:<br />{$html['comment']}<hr />"; } ?>

As you can see the nl2br() function is converting the \n to <br /> and the stripslashes() function is taking out the backslashes that escaped your quotes. The htmlentities converts all html markup into its proper form. The convention I used with the arrays for mysql and html came from Chris Shiflett of Brain Bulb which provides great PHP security information.
etono is offline   Reply With Quote
Old 05-17-2006, 06:25 AM   #3 (permalink)
Dion
Junior Member
 
Join Date: May 2006
Posts: 10
Default Re: textarea to mysql

Quote:
Originally Posted by etono
Here is how I would do it. Maybe someone else has a better method.

I would have done it the same way
Dion is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Points Per Thread View: 1.00
Points Per Thread: 11.00
Points Per Reply: 5.00



» Sponsors

» Links

» Affiliates
Web Hosting
Marketing Find
Merchant Select
SiteMap Builder
Host Compare
Dedicated Servers

» Links

» Sports Network
Paintball Forum
Football Forum
Hockey Forum
Golf Forum
Boxing Forum
Lacrosse Forum
Baseball Forum
SnowBoarding Forum
Soccer Forum
MMA Forum


All times are GMT -4. The time now is 11:04 PM.



LinkBacks Enabled by vBSEO 3.0.0 RC8
Webmaster Forums