0

我正在尝试使用户可以在文本区域中键入文本,然后更新数据库'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">
4

3 回答 3

1

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

$content = stripslashes($content);也没有意义,因为你不再使用$content任何东西,更不用说你应该通过 PHP config 或 .htaccess 禁用 magic_quotes_gpc 而不是签入代码。

最后,您可以接受 SQL 注入攻击。

编辑:正如@a​​ndrewsi 所发现的,您的<textarea>元素name也缺乏。

于 2012-10-24T15:46:14.543 回答
0

问题在于您的文本区域没有 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">
于 2012-10-24T16:13:12.143 回答
0

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

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

于 2013-05-17T01:49:13.523 回答