将文本区域保存到 mySQL 数据库字段 PHP
您好,我正在使用 openWYSIWYG 作为文本区域的文本编辑器。然后,我尝试将文本区域的内容发布到数据库中的字段。
这是我到目前为止的代码 -
<?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());
?>
我可以连接到数据库,当我单击“提交”时,它会在该字段中添加一个空白条目?我该如何获取它以保留所有格式化数据?
非常感谢
更新
<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
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 = $_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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试如下操作:
Try something like the following:
您必须将格式编码版本保存在隐藏文本区域的其他位置(与 StackOverflow 上的此处非常相似,如果您输入
**text**
,它将显示为 text,在数据库,他们可能将其保存为**text**
并使用 PHP 呈现它。保存格式化版本后,当您从数据库获取数据时使用 PHP 呈现它。
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.
您的表单是 POSTing 还是 GETing(您在帖子中提到了 POSTing)?您有 $_GET['Comments'],但如果您的表单操作是 POST,则需要使用 $_POST['Comments'];
如果在分配 $text 之后添加
echo $text;exit;
,您会看到什么吗?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?您应该使用 mysql_escape_string 函数,因为 mysql 注入和 if文本包含
'
你会得到错误。You should use mysql_escape_string function because of mysql injection and if text contains
'
you'll get error.name='Comments'
属性。mysql_real_escape_string
转义$text
。它可以包含 SQL 非法符号,例如'
。此函数使用\
转义它们mysql_*
,它已在 PHP 5.3 中弃用并将被删除。<form action='get'>
. If it is just<form>
get
used by defaultname='Comments'
attribute.$text
withmysql_real_escape_string
. It can contain SQL-illegal symbols, like'
. This function escapes them with\
mysql_*
, it is deprecated of PHP 5.3 and will be removed.只需添加 onclick 事件,例如
记住
#txtEditor
必须与表单 id 匹配,这效果很好,并注意.html
会将其保存到带有颜色的数据库中,如果您添加了任何内容(即所见即所得功能),那么对于发送到数据库的 php 代码,请执行类似
$anything
的操作,将其用作变量,不要忘记txtEidtor
是表单 ID。这样你的所见即所得就可以启动并工作了。just add the onclick event something like
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
you which to use as variable,dont forget thetxtEidtor
is the form id. with this your wysiwyg is up and working.