954,560 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

textarea value to mysql using php

I need to store the values of a textarea tag using a form to submit the value and finally store to a mysql database table. Here is what I have, I am sure its something dumb, but its been a while since I have used php.

<?php 
if(isset($_POST['submit'])){
	$con = mysql_connect ('localhost','username','password') or die(mysql_error()); 
        mysql_select_db('database') or die(mysql_error());
	//$desc = mysql_real_escape_string($_POST['txtDesc']);//nl2br($_POST['txtDesc']);
	$desc = mysql_real_escape_string($_POST['txtDesc']);
	$sql = "INSERT INTO desc_tbl (desc) VALUES('$desc')";
	$result = mysql_query($sql) or die(mysql_error());
}
?>
<form method="POST" action="textarea.php">
<textarea name="txtDesc" row = "50" cols = "50" id="txtDesc" wrap="hard"></textarea>
<input type="submit" value="submit" name="submit">
</form>


Not sure what is the issue, some extra characters in the text area? I have printed the values and I don't see any extra characters that might be causing the problem. I keep getting the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'description) VALUES('this a real escape string test')' at line 1

Thanks for the help in advance.

eoliva
Newbie Poster
6 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

The column name in your code is desc but in error message it is description. Are your providing the correct column name? Also post here the resultant query by printing $sql.

mwasif
Posting Whiz
315 posts since Dec 2007
Reputation Points: 29
Solved Threads: 48
 
The column name in your code is desc but in error message it is description. Are your providing the correct column name? Also post here the resultant query by printing $sql.

Sorry about that I forgot to change the column name. i changed it for this post, thanks for pointing it out.

mysql_connect ('localhost','username','password') or die(mysql_error()); 
    mysql_select_db('database') or die(mysql_error());
	//$desc = mysql_real_escape_string($_POST['txtDesc']);//nl2br($_POST['txtDesc']);
	$desc = mysql_real_escape_string($_POST['txtDesc']);
	echo '<pre>';
	echo $sql = "INSERT INTO desc_tbl (description) VALUES('$desc')";
	echo '</pre>';
	$result = mysql_query($sql) or die(mysql_error());
}
?>
<form method="POST" action="textarea.php">
<textarea name="txtDesc" row = "50" cols = "50" id="txtDesc" wrap="hard"></textarea>
<input type="submit" value="submit" name="submit">
</form>


Input:
this is a test

Output - This is what I get when I print $sql to the screen:

INSERT INTO desc_tbl (description) VALUES('this is a test');
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'description) VALUES('this is a test')' at line 1

eoliva
Newbie Poster
6 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Use back ticks around column name

$sql = "INSERT INTO desc_tbl(`description`) VALUES('$desc')";
mwasif
Posting Whiz
315 posts since Dec 2007
Reputation Points: 29
Solved Threads: 48
 

Thank you mwasif! thats what it lacked. Its been a while since I have played with php and mysql. Thanks again.

eoliva
Newbie Poster
6 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: