textarea to mysql - Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more
Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more
Go Back   Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more > Webmaster Tech > Programming > PHP Development

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-16-2006, 02:35 PM   #1 (permalink)
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, 05:46 PM   #2 (permalink)
Junior Member
 
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, 07:25 AM   #3 (permalink)
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
Old 05-02-2009, 10:44 AM   #4 (permalink)
Junior Member
 
Join Date: Oct 2008
Posts: 60
Default Re: textarea to mysql

Very helpful information!! Thank you very much etono.
clinkpc is offline   Reply With Quote
Old 05-15-2009, 12:16 AM   #5 (permalink)
Junior Member
 
Join Date: May 2009
Location: UK
Posts: 11
Default Re: textarea to mysql

Thanks Etono for sharing the script and description here
kateloe is offline   Reply With Quote
Old 09-16-2009, 03:45 PM   #6 (permalink)
Junior Member
 
Join Date: Sep 2009
Posts: 1
Default Re: textarea to mysql

Excellent step by step process to make it work !
gricks is offline   Reply With Quote
Old 09-20-2009, 10:35 PM   #7 (permalink)
Junior Member
 
Join Date: Sep 2009
Posts: 1
Default Re: textarea to mysql

Thanks for the suggestion, I wish it had worked.

Last edited by proasag; 09-20-2009 at 10:50 PM.
proasag is offline   Reply With Quote
Old 11-05-2009, 10:22 AM   #8 (permalink)
Junior Member
 
Join Date: Jul 2009
Posts: 4
Default Re: textarea to mysql

thanks a bunch
chandan123 is offline   Reply With Quote
Old 11-08-2009, 11:44 PM   #9 (permalink)
Junior Member
 
Join Date: Nov 2009
Location: guangzhou
Posts: 32
Send a message via ICQ to seosoe Send a message via MSN to seosoe Send a message via Yahoo to seosoe
Default Re: textarea to mysql

thanks for ur imformation
seosoe is offline   Reply With Quote
Old 11-10-2009, 04:46 AM   #10 (permalink)
Junior Member
 
Join Date: Oct 2009
Posts: 35
Default Re: textarea to mysql

Textarea tags is a multiple line text input controller it is HTML code use the specific code first we have to learn basics of HTML.

Thanks,
casachucky123 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

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



» Sponsors

» Links

» Affiliates
Web Hosting
Online Backup Reviews
Marketing Find
Merchant Select
SiteMap Builder
Host Compare

» 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 10:37 AM.


Powered by vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.2
Webmaster Forums
Web Hosting | Chicago Web Hosting | Web Hosting