donglipi4495 2012-11-29 13:07
浏览 27
已采纳

按下按钮并使用包含注释的文本区域更新mysql数据库中的注释

The question might seem a little odd but I will show you what I mean :)

So I have made a webpage that contains comments that have been made by authorised users (teachers). That is another piece of code in another page so that works perfectly. Of course it should be possible for the person who made the comment to change his own comments in case of mistakes etc.

I managed to show the user his own comments with his comments in seperate text areas. The text areas have no specific name and are just called txtComment, how do I make them unique for every comment (which is flexible because comments can be added later). I think that is the issue why I cannot update the changed/adapted textareas after I press the button.

I hope you guys understand what I am trying to say :p

I changed Dutch words mostly to English so if there is a spelling error that is due to that :)

This is the code:

<?php
$sqlKlas = "Select * from tblcomments WHERE ForWho='".$_SESSION['User']."' AND ForWho='6HA1' OR VoorWie='6HA2'";
$resultKlas = mysql_query($sqlKlas) or die(mysql_error());
if (isset($_POST['btnAdaptCommentKlas'])){
    while($rowKlas = mysql_fetch_array($resultKlas)){
        $CommentNr = $rowKlas['CommentNr'];

        $sqlUpdate = "Update tblcomments SET Comment='".$_POST['txtComment']."' WHERE CommentNr=$CommentNr";
        $resultUpdate = mysql_query($sqlUpdate) or die(mysql_error());

        echo "<tr>";
        echo "<td>";
        ?><textarea name="txtComment" cols="80" rows="5"><?php echo $rowKlas['Comment']; ?></textarea><?php
        echo "</td>";
        echo "<td>".$rowKlas['ForWho']."</td>";
        echo "<td>".$rowKlas['Datum']."</td>";
        echo "</tr>";
    }
}
else {
    while($rowKlas = mysql_fetch_array($resultKlas)){
        echo "<tr>";
        echo "<td>";
        ?><textarea name="txtComment" cols="80" rows="5"><?php echo $rowKlas['Comment']; ?></textarea><?php
        echo "</td>";
        echo "<td>".$rowKlas['ForWho']."</td>";
        echo "<td>".$rowKlas['Datum']."</td>";
        echo "</tr>";
    }
}
  • 写回答

3条回答 默认 最新

  • douguan8940 2012-11-29 13:16
    关注

    I would do it a little differently - updating separate to listing out the comments. Also You would have to set the textarea name containing the comment ID, like in my example:

    <?php
    $sqlKlas = "Select * from tblcomments WHERE ForWho='".$_SESSION['User']."' AND ForWho='6HA1' OR VoorWie='6HA2'";
    $resultKlas = mysql_query($sqlKlas) or die(mysql_error());
    
    if (isset($_POST['btnAdaptCommentKlas'])){
        foreach($_POST['txtComment'] as $key => $value) {
            $sqlUpdate = "UPDATE tblcomments SET Comment='".mysql_real_escape_string($value)."' WHERE CommentNr=".(int)$key;
            $resultUpdate = mysql_query($sqlUpdate) or die(mysql_error());
        }
    }
    while($rowKlas = mysql_fetch_array($resultKlas)){
        $CommentNr = $rowKlas['CommentNr'];
        echo "<tr>";
        echo "<td>";
        ?><textarea name="txtComment[<?php echo $CommentNr; ?>]" cols="80" rows="5"><?php echo $rowKlas['Comment']; ?></textarea><?php
        echo "</td>";
        echo "<td>".$rowKlas['ForWho']."</td>";
        echo "<td>".$rowKlas['Datum']."</td>";
        echo "</tr>";
    }
    

    Also I would recommend not to use mysql_* function but use PDO or at least mysqli_* instead. Using mysql_real_escape_string() to escape the user input is highly recommended.

    Also using MVC and separating the data manipulation (updating, fetching, creation) and data presentation (in an HTML template) would be much nicer and more maintainable for further development.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,