dongpin1850 2018-10-24 13:29
浏览 67
已采纳

我想做一个像按钮,但这是不起作用的PHP mysql

this is my code but it does not work I want a code that makes a like button for every img and if they press a button the need for waarde + 1 is to be done. but why does this code not work, he connects it well with the database but he does not do anything waarde +1

what he is doing now he increases the value of the first row in the database and it does not matter which button you click

$sql="SELECT url, categorie FROM url";
if ($result=mysqli_query($conn,$sql))
{
    while ($row=mysqli_fetch_row($result)) {
        $url = $row[0];
?>
         <div class="col-12 col-sm-6 col-lg-3 isotope-item <?php echo $row[1]; ?>">
             <div class="image-gallery-item mb-4 pb-3">
                 <a href="<?php echo $row[0]; ?>" class="lightbox-portfolio">
                     <span class="thumb-info custom-thumb-info-1">
                         <span class="thumb-info-wrapper">
                             <span class="thumb-info-plus"></span>
                             <img src="<?php echo $row[0]; ?>" class="img-fluid" alt="">
                            <form action="" method="POST"> 
                                <button type="submit" value="<?php echo $url;?>" class="btn-floating  waves-effect waves-dark transparent" name="like">
                                    <i class="material-icons blue-text">thumb_up</i>
                                </button>
                            </form>

                         </span>
                     </span>
                 </a>
             </div>
         </div>                          

<?php 
}

    if(isset($_POST['like'])){
        $sql = "UPDATE url SET waarde = waarde + 1 WHERE url = '".$url."'";

        if ($conn->query($sql) === TRUE) {
            echo "Record updated successfully";
        } else {
            echo "Error updating record: " . $conn->error;
        }
        $conn->close();
    } 

    mysqli_free_result($result);
} 
mysqli_close($connection); 
?>

can somebody help me?

  • 写回答

1条回答 默认 最新

  • douyannuo7733 2018-10-25 09:44
    关注

    Change this:

    if(isset($_POST['like'])){
            $sql = "UPDATE url SET waarde = waarde + 1 WHERE url = '".$url."'";
    

    into this:

    if(isset($_POST['like'])){
            $value = $_POST['like'];
            $sql = "UPDATE url SET waarde = waarde + 1 WHERE url = '$value'";
    

    so you are parametrizing your query to the url that have been posted.

    Two side notes:

    • the update query should be out of the while loop. It is not necessary to repeat it several times;
    • you should not have the same name for a table (url) and a field in the table (url). It is not a good practice at least for readibility and can lead to issues in the query if you don't specify always the table name like url.url
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗