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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 修改光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据
  • ¥15 为什么openeluer里面按不了python3呢?
  • ¥15 关于#matlab#的问题:训练序列与输入层维度不一样
  • ¥15 关于Ubuntu20.04.3LTS遇到的问题:在安装完CUDA驱动后,电脑会进入卡死的情况,但可以通过键盘按键进入安全重启,但重启完又会进入该情况!
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解