1

i am using text area in my code to insert data in database. but there is some confusion in my code. when i click at submit button, text in textarea disappear and not saved in database. this is my Code. Form Coding:

<form action="testing.php" method="post">
 <textarea name="text1" rows="10" cols=59></textarea><br>
 <input type="submit" name="submit" value="Add Record">
 <input type="reset" name="reset" value="Clear Text">
</form>

PHP Coding:

<?php 
$text1=mysql_real_escape_string($_POST['text1']);
// connection
$con=mysql_connect("localhost","root","") or die("connection error");
mysql_select_db("test") or die("database error");
//query...
$qry="INSERT INTO mytest(text1) values($text1)";
if(mysql_query($qry))
{
echo "Record have been saved...";
}
else
{
echo "Not Saved";
}
?>
Alvi
  • 123
  • 1
  • 3
  • 14
  • 1
    No mysql question can go without the **strong encouragement** to [not use mysql_*](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?rq=1) functions. – random_user_name Sep 12 '16 at 16:16
  • Since it's a string, you may need to quote your value in the query: `INSERT INTO \`mytest\` (\`text1\`) values ('$text1')` For troubleshooting, see [mysql_error()](http://php.net/manual/en/function.mysql-error.php). – showdev Sep 12 '16 at 16:17
  • Programming 101: Turn on error reporting. Get your PHP / MySQL to output any mysql error(s) that happen. That will tell you much more about what's going wrong. – random_user_name Sep 12 '16 at 16:18

2 Answers2

2

You need to wrap it within quotes. Also, it would be better if you use some debugging along with it.

$qry = sprintf("INSERT INTO mytest (text1) VALUES ('%s') ", $text1);

if (mysql_query($qry)) {
    echo "Record have been saved...";
} else {
    echo "There was an error:".mysql_error();
}

Note: The mysql extension is deprecated. Use mysqli or PDO instead.

Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
0

this will probably work:

$qry="INSERT INTO mytest(text1) values('".$text1."')";

of course incase you have typed the following line previously:

$_POST['text1']= $text1;

and most impotantly you run the query using this function: mysql_query ( string $query [, resource $link_identifier = NULL ] )

MrMisery
  • 406
  • 7
  • 19
  • Why would that work any differently than the OP question? Interpolation will happen. Also, you don't know enough to answer this: does the form post to the right page? What happens if the OP var_dumps $text1? $qry? – random_user_name Sep 12 '16 at 16:17
  • it has been a while since I last wrote php code but as you can see , the query was not executed which i added in addition post variable was probably not used so the first point may not be as important. – MrMisery Sep 12 '16 at 16:24
  • Your second command is wrong. Should be `$text1 = $_POST['text'1];` - not the other way around. – random_user_name Sep 12 '16 at 17:19