dow58115 2014-01-21 22:54
浏览 4
已采纳

都是talene进入数据库 - 它应该只用一个数字

that's how I'm making my own little "like" system for my website, this is how when I click "like" then throw it all id into the database so it means that I have 1, 2 , 3, 4, and 5, so if for example, I click on 5 so smidre on all the others into the database even though I did not click on them.

so the problem is that it throws some numbers into the database which I have not looked

    $sql = "SELECT bruger.fornavn, bruger.efternavn, bruger.img, wall.id, wall.tekst, wall.brugerid, wall.opret_dag FROM wall JOIN bruger ON wall.brugerid=bruger.id ORDER BY `wall`.`id` DESC";
if ($stmt = $this->mysqli->prepare($sql))
{ 
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($fornavn, $efternavn, $img, $id, $tekst, $brugerid, $opret_dag);
    while ($stmt->fetch())
    {
    ?>
    <div class="statwall">
        <h2>Test</h2>
        <div class="wallindhold">
            <?php               
            echo $tekst;
            ?>
        </div>
            <?php
            if($_SESSION["login"] != false and $_SESSION["rank"] == 1)
            {
            ?>
            <form action="#" method="post">
                <input type="submit" name="like" value="Synes godt om" id="likebutton">
            </form>
            <?php
                if(isset($_POST["like"]))
                {
                    if($stm = $this->mysqli->prepare('INSERT INTO wallLike (getid, brugerid, dato_time) VALUES (?, ?, now())' ))
                    {
                        $stm->bind_param('ss', $getid, $brugerid);
                        $getid = $id;
                        $brugerid = $_SESSION["id"];
                        $stm->execute();

                        echo "No Error!";

                        $stm->close();
                    }
                    else
                    {
                        echo 'Der opstod en fejl i erklæringen: ' . $this->mysqli->error;
                    }
                }
            }
            ?>
            <div style="clear:both;"></div>
        </div>
        <?php
    }
    $stmt->close();
}

The problem is that it throws all the numbers into the database. what I want to only throw a number into the database possibly the figures that I have clicked.

  • 写回答

1条回答 默认 最新

  • dongyounai6281 2014-01-21 23:50
    关注

    Your page currently has lots of forms, but they are all exactly the same. Whichever one you click, the values you get will be the same, so the same thing will happen. You check if a form has been submitted here:

    if(isset($_POST["like"]))
    

    But there is nothing to tell which form was submitted. So if a form was submitted, that will always be true, and the code will insert things into the database.

    First, you need to make your forms different in some way. For instance, add a hidden input with the value of $id in:

            ?>
            <form action="#" method="post">
                <input type="submit" name="like" value="Synes godt om" id="likebutton">
                <input type="hidden" name="like_id" value="<?php echo $id; ?>" />
            </form>
            <?php
    

    Second, you need to check which form was submitted:

     if( isset($_POST["like"]) && $_POST["like_id"] == $id )
    

    Note that you could have two completely separate loops, one displaying the forms, and one processing the database INSERTs after the form has been submitted. This is often recommended, because it makes it clearer when different things are happening.

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

报告相同问题?

悬赏问题

  • ¥15 神经网络模型一直不能上GPU
  • ¥15 苍穹外卖拦截器token为null
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵
  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了
  • ¥15 陆空双模式无人机怎么做
  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
  • ¥15 C#中的编译平台的区别影响