-1

i want to add commenting to each article, so inside my"foreach"cycle I added commenting to each article, but "set a comment" function runs to all art

thats the code for making an article window

        <?php $articles_qr = mysqli_query($connection, "SELECT * FROM `articles` ");
    $articles = array();
    while ( $art = mysqli_fetch_assoc($articles_qr))
                    {
                        $articles[] = $art;
                    }

 ?>
            <?php foreach ($articles as $art)
                    {
             ?>
    <section>
        <div class="containerstuff">

            <div class="stuffpic">
            <img src="<?php echo "../static/imagespages/",$art['image'] ?>"  class="pico">

            </div>
            <div class="article">
                <h1><?php
                        echo $art['title']
                    ?>
                </h1>
                <?php
                        echo $art['text'];
                        echo $art['id']
                    ?>

            </div>
            </div>

        <div class="scrollmenu">

            <?php include "../includes/comments.php";?>

    </section>
        <?php
        } ?>

thats the code comments window

        <?php
date_default_timezone_set(timezone_identifier);
include_once '../comments.ink.php'

?>
<div class="containercom">

                <img src="#" class="commpic">
                <p class="comment"></p>
            </div>


            <div class="blockcom">



                <form class='form' method='POST' action="<?php echo setComments($connection)?>">

                    <div class='form__group'>

                        <input type='hidden' name='page_id' value="<?php echo $art['id']?>" >

                        <input type='hidden' name='uid' value='Anonymous'>
                        <input type='hidden' name='pubdate' value="<?php echo date('Y-m-d H:i:s')?>" >
                        <textarea name='text' class='form__control' placeholder ='Введите комментарий...' required=''></textarea>

                    </div>
                        <div class='form__group'>
                            <input type='submit' class='form__control2' name='commentSubmit'></input>
                        </div>
                    </div>
                </form>

and thats the code for INSERT function

<?php

        static $firstTime = true;
function setComments($connection) {
    if(isset($_POST['commentSubmit'])){
        $idcom = $_POST['page_id'];
        $uid = $_POST['uid'];
        $pubdate = $_POST['pubdate'];
        $text = $_POST['text'];

        $sql =
        "INSERT INTO `comments` (page_id, uid, pubdate, text)
        VALUES ('$idcom', '$uid', '$pubdate', '$text')";

        $result = $connection->query($sql);
        $firstTime = false;

    }

}

so how can i make insert only for 1 article (so when i add it now, there are appears as many comments as many articles i have in database)

  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman May 14 '21 at 20:50

2 Answers2

0

I think you should use ajax to append a new comment which is the widely used solution, the way u r doing will become difficult to handle for you.

Rashid Mirza
  • 1
  • 1
  • 2
0

I haven't found the solution, so I just selected another way.

For each article I placed a button to post a comment which will send user to the page with this article and for this button for "href" I wrote php code (href = "comments.php?id=<?php echo $art['id']"?>) and for this page I use $_GET to select articles only for this id. Then I just placed there comments-function that I wrote so it works alright now because function works only for 1 argument

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459