dongyun7571 2016-03-24 22:06
浏览 30
已采纳

为数据创建唯一变量

Hi guys so i have created a simple comment box for my site now. It works perfectly, however the problem i am having is that i have different pages which are going to require different comment box. I cant seem to figure out how to get the comment box to be unique for every page. So right now my database holds this :

Called comments: 
id 
comment
comment1
comment_date

Now my idea is that everything was stored into comment, so i added comment1 for other page to store the info. However i have no clue how to edit the php file to get it to work with comment1. Any help on this would be great.

HTML:

<div class="comment_container">
    <div class="comments">
        <?php
            include_once("comments.php");
        ?>
    </div>
        <div class="comments_form">
        <table>
        <tr><td><textarea id="comment_text"></textarea></td>
        <td><input type="button" id="comment_process" value="Post Comment"/></td></tr>
        </table>
    </div>
</div>

JS:

$(document).ready(function() {
        $('#comment_process').click(function() {
            if ($('#comment_text').val() != "") {
                $.post("comments.php?action=post", {
                    comment: $('#comment_text').val()
                }, function(data) {
                    $('.comments').html(data);
                    $('#comment_text').val("");
                });
            }
        });
          });

PHP:

include_once("connect.php");

function convert ($date) {
    $converteddate = date("F j, Y g:ia", strtotime($date." +1day"));
    return $converteddate;
}

function getComments(){
    $comments = "";
    $sql = mysql_query("SELECT * FROM comments") or die(mysql_error());
    if(mysql_num_rows($sql) == 0){
        $comments = "<div class='each_comment'>There are no comments</div>";
    } else {
        while($row = mysql_fetch_assoc($sql)){
            $comments .= "<div class='each_comment'><small><em>".convert($row['comment_date'])."</em></small><br />".$row['comment']."</div>";
        }
    }
    return $comments;
    } 

function postComments($comment){
    $comment = mysql_real_escape_string(strip_tags($comment));
    $sql = mysql_query("INSERT INTO comments (comment, comment_date ) VALUES ('".$comment."', now())");
    return true;
}
    if((isset($_GET['action'])) && ($_GET['action']== "post")){
        postComments($_POST['comment']);
    }
    echo getComments();

Thanks again for the help

  • 写回答

1条回答 默认 最新

  • douxian9010 2016-03-24 22:49
    关注

    DISCLAIMER
    For future visitors: Don't copy this code, as it has several issues that go beyond answering the question.

    What you need to add is an identifyer for the type of comment. (Type could be replaced with something more suitable to your case like 'product', 'user', ... whatever the difference is/what they are related to)

    So in your database add that new column:

    comments
    --------
    id
    comment
    type
    comment_date
    

    Now you need to pass around that type through all your calls, and it shall be specified in your 'HTML'-Page (which actually is php...).

    <div class="comment_container">
      <div class="comments">
        <?php
            // specify the type needed on that page
            $type = 1;
            include_once("comments.php");
            echo getComments($type);
        ?>
      </div>
      <div class="comments_form">
        <table>
        <tr><td><textarea id="comment_text"></textarea></td>
        <td><input type="button" id="comment_process" value="Post Comment"/></td></tr>
        </table>
      </div>
    </div>
    <script>
        // specify the type in javascript
        var type=1;
        $(document).ready(function() {
          $('#comment_process').click(function() {
            if ($('#comment_text').val() != "") {
                                             // add the type here:
                $.post("comments.php", {
                    comment: $('#comment_text').val(),
                    type: type,
                    action: 'post'
                }, function(data) {
                    $('.comments').html(data);
                    $('#comment_text').val("");
                });
            }
          });
        });
    </script>
    

    and in comments.php:

    //....some code left out here
    function getComments($type){
       $comments = "";
       $sql = mysql_query("SELECT * FROM comments where type=$type") or die(mysql_error());
       if(mysql_num_rows($sql) == 0){
           $comments = "<div class='each_comment'>There are no comments</div>";
       } else {
           while($row = mysql_fetch_assoc($sql)){
               $comments .= "<div class='each_comment'><small><em>".convert($row['comment_date'])."</em></small><br />".$row['comment']."</div>";
           }
       }
       return $comments;
    } 
    
    function postComments($comment, $type){
       $comment = mysql_real_escape_string(strip_tags($comment));
       $sql = mysql_query("INSERT INTO comments (comment, comment_date, type ) VALUES ('".$comment."', now(), ".$type.")");
    return true;
    }
    if((isset($_POST['action'])) && ($_POST['action']== "post")){
        postComments($_POST['comment'], $_POST['type']);
        // send all the comments back to client
        echo getComments($_POST['type']);
    }
    // moved to html-file: echo getComments($type);
    

    NOTE

    There are several issues with that code.
    First don't use mysql functions. For real. Unsecure and deprecated/deleted as of php7. Use mysqli or pdo. Furthermore your sql can be hacked with sql injection. Read about prepared statements.

    The general structure of that code is not very good. Try to seperate output and formating from getting data. For example it would be much better if a function called 'getComments' only would get the comments from the database, then let others decide what to do with that data. The less one function does the better.
    Please read about coding styles, maybe start learning object oriented programming.

    I hope this still helps you to get a clue of where to go!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看