-2

updateupdate

Can anyone explain to me why I am getting duplicate messages instead of one? how can I change my code so that when I type a comment and press "Comment" button, it will only display one message instead of duplicates! When I have one comment boxes it doesn't show duplicate comments, but if I have more than one then it starts duplicating!

COMMENT.INC.PHP

include 'cdbh.inc.php';
function setComments($con) 
{
    if (isset($_POST['commentSubmit'])) {
        $uid = mysqli_real_escape_string($con,$_POST['uid']);
        $date = mysqli_real_escape_string($con,$_POST['date']);
        $message =  mysqli_real_escape_string($con,$_POST['message']);

        $sql = "INSERT INTO comments (uid, date, message) VALUES ('$uid','$date','$message')";
        $result = mysqli_query($con,$sql);
    }
}

function getComments($con)
{
    $sql = "SELECT * FROM comments";
    $result = mysqli_query($con,$sql);
    while ($row=mysqli_fetch_assoc($result)) {
        echo $row['uid'];
        echo ":"; 
        echo $row['message']."<br><br>";
    }
}

page code

<?php
    date_default_timezone_set('America/Los_Angeles');  
    include 'comment.inc.php';
    include("connection.php");
?>
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
            <link href="comment.css" rel ="stylesheet">
        </head>
        <body>
        <?php
            $sql="Select * from tbl_images";
            $result=mysqli_query($connection,$sql);
            while ($row=mysqli_fetch_array($result)) {
        ?>
        <img src="images/<?php echo $row['images_name'] ?>" width="200px" height="200px">
        <?php
            echo "<form method ='POST' action ='".setComments($con)."'>
                    <input type ='hidden' name ='uid' value='unknown'>
                    <input type ='hidden' name ='date' value='".date('Y-m-d H:i:s')."'>
                    <textarea name='message'></textarea>
                    <button type ='submit' name='commentSubmit'>Comment</button>
                </form>";
            }
            getComments($con);
        ?>
    </body>
</html>
Denny Feng
  • 19
  • 5
  • Are you using ajax / jQuery to submit the form? If so, you should post the javascript as well. – jeroen Jul 04 '16 at 08:37
  • Debug your own code step by step using echo's and exit's.... – Mangesh Sathe Jul 04 '16 at 08:45
  • 1
    Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared statement and parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Jul 04 '16 at 08:45
  • You are doing `SELECT * FROM comments` maybe you should be limiting that query to comments for a specific user i.e. `SELECT * FROM comments WHERE uid = ?` – RiggsFolly Jul 04 '16 at 08:49
  • Thanks for the replies, but the comments are duplicating inside my http://localhost/phpmyadmin/! If I only use one comment box and one comment button, it will only display a single comment on the database. However, let's say I have three comment boxes, given inside the picture above. When I write a comment on one of the boxes and pressed the button, it will duplicate two more of the exact comments in my database. When I have taken out "getComments($con);" and tried it again, it still have the same problem! – Denny Feng Jul 04 '16 at 19:10

1 Answers1

-1

Maybe you are submiting all your forms instead of one.. check your database in order to know from what img comes each message. If you have other code like javascript, you should post it.

singe batteur
  • 401
  • 2
  • 14
  • 2
    This is a comment and not an answer. Please dont post comments as answers, people tend to downvote this. You only ned 50 points to be able to comment on anything, till then stick to well asked questions that need no clarification – RiggsFolly Jul 04 '16 at 08:50