Saving text area into mySQL database field PHP
Hi i am using openWYSIWYG as a text editor for a text area. I then am trying to post the contents of the text area to a field in my database.
This is the code i have so far -
<?php
$text开发者_JAVA技巧 = $_GET['Comments'];
mysql_connect ("localhost", "user", "password") or die ('Error: ' . mysql_error());
mysql_select_db("databasename") or die ('Data error:' . mysql_error());
$query="INSERT INTO KeepData (player_data)VALUES ('$text')";
mysql_query($query) or die ('Error updating database' . mysql_error());
?>
I can connect to the database, and when i click submit it adds a blank entry into the field? how would i get it so it keeps all the formatted data?
Many thanks
update
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<textarea id="Comments" name="Comments">
example text
</textarea>
<input type="submit" name="mysubmit" value="Save Post" />
</form>
DIM3NSION
Try something like the following:
<?php
if ($_POST['submit']) {
mysql_connect ("localhost", "user", "password") or die ('Error: ' . mysql_error());
mysql_select_db("databasename") or die ('Data error:' . mysql_error());
$text = mysql_real_escape_string($_POST['comments']);
$query="INSERT INTO KeepData (player_data) VALUES ('$text')";
mysql_query($query) or die ('Error updating database' . mysql_error());
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<textarea name="comments">Example Comment</textarea>
<input name="submit" type="submit" value="submit" />
</form>
You must save an format coded version elsewhere on a hidden textarea (much like here on StackOverflow, if you type **text**
it will come out as text, in the database, they probably save it as **text**
and render it with PHP.
Once the formatted version is saved, render it with PHP when you get the data from the database.
Is your form POSTing or GETing (you said POSTing in your post)? You have $_GET['Comments'], but if your form's action is POST, you need to use $_POST['Comments'];
if you add echo $text;exit;
after you assign $text, do you see anything?
You should use mysql_escape_string function because of mysql injection and if text contains '
you'll get error.
- Check if your have
<form action='get'>
. If it is just<form>
get
used by default - Check that your wisywig have
name='Comments'
attribute. - Escape the
$text
withmysql_real_escape_string
. It can contain SQL-illegal symbols, like'
. This function escapes them with\
- (recommendation) do not use
mysql_*
, it is deprecated of PHP 5.3 and will be removed. - (recommendation) appending user input to sql query is always a risk of SQL-injection. Use prepared statements
just add the onclick event something like
<button onclick=" $('#txtEditor').val($('.Editor-editor').html());" type="Publish" id="Publish" name="Publish" class="btn btn-primary">Publish</button>
remember the #txtEditor
has to match with the form id, this works well, and note the .html
will save it to database with the color,Bold and many more effect if you added any (that is the wysiwyg fuction)
then for your php code that send to database, do something like this
$anything = ($_POST['txtEditor']);
$anything
you which to use as variable,dont forget the txtEidtor
is the form id. with this your wysiwyg is up and working.
精彩评论