php - 使用 AJAX 单击将 mysql 值加一?

标签 php html mysql ajax

我对 AJAX 和 PHP 非常陌生。我正在尝试在用户可以发布模因的网站上实现“喜欢”功能。用户应该能够单击某个 meme 旁边的心形图像来“喜欢”,并且该特定 meme 的“喜欢”值应该在 MySQL 数据库中增加 1,但是,它没有执行任何操作。

MySQL 表名为 meme_information,它有 5 列,“name”、“email”、“likes”、“image_path”和“id”(主键且自动递增)。

下面是显示所有表情包的页面的代码。上面有一些 html 我遗漏了,但它只是导航栏和文件中的来源。我很确定 AJAX 中的“数据”值是错误的,但我不确定应该在其中放入什么。

浏览.php

<?php 
include("connecttodb.php");

$files = glob("uploads/*.*");

for ($i=0; $i<count($files); $i++) {
    $image = $files[$i];
    $supported_file = array(
            'gif',
            'jpg',
            'jpeg',
            'png'
    );

    $image = $files[$i];

    $path = substr($image,8);

    $q = "SELECT name, email, id FROM meme_information WHERE image_path= \"$path\"";

    $id = 0;
    $result = $link->query($q);
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            echo "Name: ". $row["name"] ."<br>";
            echo "Email: ". $row["email"] ."<br>";
            echo "Id: ". $row["id"] . "<br>";
            $id = $row["id"];
        }
    }

    echo '<img src="'.$image.'" style="width:400px;height:400px;"></img>';

    echo '<img onclick="myFunction()" src="heart-unclicked.png" style="width:50px;height:50px;">like</img>';

    echo
        '<script>
            function myFunction() {
                $.ajax({
                    type: "POST",
                    url: "update_likes.php",
                    data: {"increment_by": 1},
                    success: function() {
                        alert("works");
                    },
                    error: function() {
                        alert("doesnt work");
                    },
                });
            }
        </script>';   
}
?>

我认为 SQL 查询中 WHERE 之后的部分是错误的。不知道如何从“喜欢”的模因中获取 ID 号。

update_likes.php

<?php
    require('connecttodb.php');

    $q2 = "UPDATE meme_information SET likes = likes + 1 WHERE id='".$id."'";
    echo "The id is $id" . "<br>";
    $result = mysqli_query($link, $q2);
    echo "<br> this is the id" . "$result";
?>

connecttodb.php

<?php
global $link;   

include("dbconnect.php");

$link = new mysqli($server,$user,$password,$dbname);

if ($link->connect_errno) {
    die("Connection failed: " . $link->connect_error);
} else {
    print"Connection successful.";
}

?>

最佳答案

这可能会解决问题。

<?php 
include("connecttodb.php");

$files = glob("uploads/*.*");

for ($i=0; $i<count($files); $i++) {
    $image = $files[$i];
    $supported_file = array(
            'gif',
            'jpg',
            'jpeg',
            'png'
    );

    $image = $files[$i];

    $path = substr($image,8);

    echo '<img src="'.$image.'" style="width:400px;height:400px;"></img>';

    $q = "SELECT name, email, id FROM meme_information WHERE image_path= \"$path\"";

    $id = 0;
    $result = $link->query($q);
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            echo "Name: ". $row["name"] ."<br>";
            echo "Email: ". $row["email"] ."<br>";
            echo "Id: ". $row["id"] . "<br>";
            $id = $row["id"];

            echo '<img onclick="myFunction('.$id.')" src="heart-unclicked.png" style="width:50px;height:50px;">like</img>';

        }
    }


    echo
        '<script>
            function myFunction(id) {
                $.ajax({
                    type: "POST",
                    url: "update_likes.php",
                    data: {"increment_by": 1,"id": id},
                    success: function() {
                        alert("works");
                    },
                    error: function() {
                        alert("doesnt work");
                    },
                });
            }
        </script>';   
}
?>

update_likes.php

<?php
    require('connecttodb.php');
    if(isset($_POST['id']))
{
    $q2 = "UPDATE meme_information SET likes = likes + 1 WHERE id='".$_POST['id']."'";
    echo "The id is $id" . "<br>";
    $result = mysqli_query($link, $q2);
    echo "<br> this is the id" . "$result";
}
?>

关于php - 使用 AJAX 单击将 mysql 值加一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49142589/

相关文章:

php - 现在允许使用 CakePHP 中的插件 Authake 执行操作

javascript - 选择框值作为 ID 而不是数据

html - 拆分背景 - 在面板展开时停止移动

php - 将 2 列中的精确串联值分组?

MySQL左连接子查询空连接

php - 从外键检索数据,一对一关系。拉维尔

php - 在 where 条件中使用括号 ()、星号 *.. 等特殊字符

javascript - 如何划分输入字段以在屏幕上显示为单独的输入字段?

javascript - 如何在内联粗体标记后对元素进行范围选择?

mysql - 如何自动删除数据库表中的旧记录?