将文本区域保存到 mySQL 数据库字段 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7040101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving text area into mySQL database field PHP
提问by DIM3NSION
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.
嗨,我使用 openWYSIWYG 作为文本区域的文本编辑器。然后我试图将文本区域的内容发布到我的数据库中的一个字段。
This is the code i have so far -
这是我到目前为止的代码 -
<?php
$text = $_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
维度
回答by Mat
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>
回答by Madara's Ghost
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.
您必须在隐藏文本区域的其他地方保存格式编码版本(很像 StackOverflow 上的这里,如果您键入**text**
它,它将作为text 出现,在数据库中,他们可能会将其另存为**text**
并使用 PHP 呈现。
Once the formatted version is saved, render it with PHP when you get the data from the database.
一旦格式化的版本被保存,当你从数据库中获取数据时,用 PHP 渲染它。
回答by Benno
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'];
您的表单是 POSTing 还是 GETing(您在帖子中说 POSTing)?你有 $_GET['Comments'],但如果你的表单的 action 是 POST,你需要使用 $_POST['Comments'];
if you add echo $text;exit;
after you assign $text, do you see anything?
如果echo $text;exit;
在分配 $text 后添加,您看到什么了吗?
回答by kravemir
You should use mysql_escape_stringfunction because of mysql injection and if text contains '
you'll get error.
由于 mysql 注入,你应该使用mysql_escape_string函数,如果文本包含'
你会得到错误。
回答by J0HN
- 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
- 检查您是否有
<form action='get'>
. 如果只是<form>
get
默认使用 - 检查您的 wisywig 是否具有
name='Comments'
属性。 - 逃脱
$text
与mysql_real_escape_string
。它可以包含 SQL 非法符号,例如'
. 这个函数用\
- (推荐)不要使用
mysql_*
,它在 PHP 5.3 中已被弃用并将被删除。 - (推荐)将用户输入附加到 sql 查询总是存在 SQL 注入的风险。使用准备好的语句
回答by youngdero
just add the onclick event something like
只需添加类似的 onclick 事件
<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)
记住#txtEditor
必须与表单 id 匹配,这很好用,并注意.html
如果添加任何颜色,粗体和更多效果(即所见即所得功能),它将保存到数据库中
then for your php code that send to database, do something like this
然后对于发送到数据库的 php 代码,做这样的事情
$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.
$anything
您要用作变量,不要忘记txtEidtor
是表单ID。有了这个,你的所见即所得就可以正常工作了。