Php 是否使用文本区域更新SQL表?
我正在尝试这样做,用户可以在textarea中键入文本,然后更新数据库“ptb_profiles”中的mysql表“bio” 目前,他们的信息正在被拉入这个文本区域,但我想要它,以便他们在其中键入的任何内容都能更新表 我正在使用以下代码,但它不工作?我是php和mysql新手,所以肯定是错的 任何帮助都将不胜感激。谢谢Php 是否使用文本区域更新SQL表?,php,mysql,textarea,Php,Mysql,Textarea,我正在尝试这样做,用户可以在textarea中键入文本,然后更新数据库“ptb_profiles”中的mysql表“bio” 目前,他们的信息正在被拉入这个文本区域,但我想要它,以便他们在其中键入的任何内容都能更新表 我正在使用以下代码,但它不工作?我是php和mysql新手,所以肯定是错的 任何帮助都将不胜感激。谢谢 <?php //We check if the form has been sent if(isset($_POST['bio'])) { $content =
<?php
//We check if the form has been sent
if(isset($_POST['bio']))
{
$content = $_POST['bio'];
//We remove slashes depending on the configuration
if(get_magic_quotes_gpc())
{
$content = stripslashes($content);
}
//We check if all the fields are filled
if($_POST['bio']!='')
{
$sql = "INSERT INTO ptb_profiles.bio VALUES (NULL, '".$_SESSION['user_id']."', '".$message['bio']."');";
mysql_query($sql, $connection);
echo "<div class=\"infobox-message\">Your Profile Information has been updated..</div>";
}
}
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<textarea id="bio" rows="10" style="width: 456px;
margin-top:3px;
text-align:left;
margin-left:-2px;
height: 122px;
resize: none;
border: hidden;"><?php echo $profile['bio'] ?> </textarea>
<input type="submit" name="send_button" id="send_button" value="Send">
您正在插入
$message['bio']
的内容,但代码中没有$message
数组(也没有用于填充下面HTML的$profile
)
还有您的$content=stripslashes($content)
毫无意义,因为您不再使用$content
,更不用说您应该通过PHP config或.htaccess禁用magic_quotes\u gpc,而不是签入代码
最后,您将面临SQL注入攻击
编辑:正如@andrewsi所指出的,您的
元素也缺少名称。问题在于您的文本区域没有name=“bio”属性。还可以使用nl2br和htmlentities,然后保存到数据库中。当你把它拿回来的时候,把真正的实体拿回来
<?php
//We check if the form has been sent
if(isset($_POST['bio']))
{
echo $content = htmlentities(nl2br($_POST['bio']));
//We remove slashes depending on the configuration
//We check if all the fields are filled
$sql = "INSERT INTO ptb_profiles.bio VALUES (NULL, '".$_SESSION['user_id']."', '".$content."');";
mysql_query($sql, $connection);
echo "<div class=\"infobox-message\">Your Profile Information has been updated..</div>";
//exit;
}
$sql = "Select bio from ptb_profiles.bio WHERE user_id = '".$_SESSION['user_id']."'";
$res = mysql_query($sql, $connection);
$profile = mysql_fetch_array($res);
$nl = str_replace("<br />", "\n", $profile['bio']);
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<textarea id="bio" name="bio" rows="10" style="width: 456px;
margin-top:3px;
text-align:left;
margin-left:-2px;
height: 122px;
resize: none;
border: hidden;"><?php echo $nl; ?> </textarea>
<input type="submit" name="send_button" id="send_button" value="Send">
当我自己遇到这个问题,并且花了一些时间尝试用许多论坛帖子的解决方案来解决问题时,我设法解决了这个问题。在我检查过的所有论坛中,问题是使用$\u POST php超全局变量将字符串值从textarea获取到sql“INSERT”语句中
我所能看到的真正有效且不会抛出任何错误的唯一方法是在编写“INSERT”语句时忽略列名,而不是:
"INSERT INTO tbl_dbTable ( Colummn1, Colummn2...) VALUES( '$_POST[textarea]', 'Value2'..)"
只需使用:
"INSERT INTO tbl_dbTable VALUES( '$_POST[textarea]', 'Value2'..)"
唯一的缺点是必须列出表中所有列的值,但它解决了将textarea值放入sql列的问题
希望这能帮助其他有同样问题的人什么是“它不起作用”呢?没有向数据库写入任何内容?网站崩溃?写错误的信息?将正确的信息写入错误的位置?我单击“提交”,它只是宿舍,不起任何作用。0 null绝对为零:/他的textarea字段也没有name
属性,只有id
;这也可能会引起一个问题。