2012-10-24 16 views
0

我试图让用户可以在textarea中输入文本,然后更新数据库'ptb_profiles'内的mysql表'bio'。更新带有文本区域的SQL表?

目前他们的信息被拉进这个textarea,但我想要它,所以无论他们键入它更新表。

我正在使用下面的代码,但它不工作?即时通讯新的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

它是如何工作? –

+0

'它不工作'是什么意思?不写任何东西到数据库?崩溃的网站?写错信息?将正确的信息写入错误的地方? – andrewsi

+0

我点击提交,它只是休眠,什么都不做。 0 null绝对没有。 :/ –

回答

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

要插入的$message['bio']内容但没有$message阵列在你的代码(也$profile你用下面填入HTML)。

此外,您的$content = stripslashes($content);是没有意义的,因为您不再使用$content,更不用说您应该通过PHP配置或.htaccess禁用magic_quotes_gpc而不是检查代码。

最后你打开SQL注入攻击。

编辑:如@andrewsi发现,您的<textarea>元素也缺少name

+1

他的textarea字段也没有'name'属性,只有'id';这也可能导致问题。 – andrewsi

0

在有这个问题我自己,花一些时间来尝试从很多很多论坛的帖子我设法解决这个同样的问题,解决的解决方案的问题。在所有论坛中,我检查问题是从textarea获取字符串值到使用$ _POST php超全局变量的sql'INSERT'语句

唯一的方法是我可以看到实际工作并没有抛出任何错误是我在写“INSERT”语句时,所以不是忽略列名:

"INSERT INTO tbl_dbTable (Colummn1, Colummn2...) VALUES('$_POST[textarea]', 'Value2'..)" 

简单地使用:

"INSERT INTO tbl_dbTable VALUES('$_POST[textarea]', 'Value2'..)" 

唯一的缺点是你必须再为表中的所有列列表值,但它解决了获取问题的问题textarea值转换成一个sql列。

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