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.

Recommended Answers

All 7 Replies

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.

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

Use back ticks around column name

$sql = "INSERT INTO desc_tbl(`description`) VALUES('$desc')";

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

No it did not solve still!!!
HAd similar problem tried ur soilution but error:
query task add FAILED becauseYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''taskId', 'employeeId', 'name', 'projectId', 'date', 'dueDate', 'leaderId','desc' at line 1

Member Avatar for iamthwee

order by desc?

thanks for snippet.
What if we want to insert php code in mysql via "textarea" ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.