【问题标题】:Update SQL Table with Text Area?用文本区域更新 SQL 表?
【发布时间】:2012-10-24 15:43:15
【问题描述】:

我正在尝试让用户可以在 textarea 中键入文本,然后更新数据库 'ptb_profiles' 中的 mysql 表 'bio'。

目前他们的信息被拉入这个文本区域,但我想要它,以便他们在其中输入的任何内容都会更新表格。

我正在使用以下代码,但它不起作用?我是 php 和 mysql 新手,所以肯定是错误的。

任何帮助将不胜感激。谢谢。

<?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">

【问题讨论】:

  • “它不工作”是什么意思?不向数据库写入任何内容?网站崩溃?写错信息?将正确的信息写入错误的位置?
  • 我点击提交,它只是休眠,什么也没做。 0 null 绝对没有。 ://

标签: php mysql textarea


【解决方案1】:

您正在插入 $message['bio'] 的内容,但您的代码中没有 $message 数组(也没有 $profile 用于填充下面的 HTML)。

您的$content = stripslashes($content); 也毫无意义,因为您不再使用$content,更不用说您应该通过PHP 配置或.htaccess 禁用magic_quotes_gpc 而不是签入代码。

你终于可以接受 SQL 注入攻击了。

编辑:正如@a​​ndrewsi 所发现的,您的&lt;textarea&gt; 元素也缺少name

【讨论】:

  • 他的 textarea 字段也没有name 属性,只有id;这也可能导致问题。
【解决方案2】:

问题在于您的文本区域没有 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">

【讨论】:

    【解决方案3】:

    在我自己遇到这个问题并花了一些时间尝试通过许多论坛帖子的解决方案解决问题后,我设法解决了同样的问题。在我检查的所有论坛中,问题是使用 $_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 列的问题。

    希望这可以帮助其他遇到同样问题的人

    【讨论】:

      猜你喜欢
      • 2012-10-28
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多