使用AJAX点击一个mysql值增加一个?

时间:2018-03-07 01:10:13

标签: php html mysql ajax

我是AJAX和PHP的新手。我试图实现一个"喜欢"用户可以发布模因的网站上的功能。用户应该能够点击一个心形图像,以及#34;喜欢"紧挨着模因和"喜欢"在MySQL数据库中,该特定模因的值应该增加1,但它没有做任何事情。

MySQL表名为meme_information,它有5列,"名称","电子邮件","喜欢"," image_path"和" id"这是主键和自动增量。

以下是显示所有模因的页面代码。我遗漏了一些上面的html,但它只是导航栏和文件源。我很确定"数据" AJAX中的值是错误的,但我不确定我应该放在那里。

browse.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之后的部分是错误的。不知道如何从#34;喜欢的模因中获取身份证号码。&#34;

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.";
}

?>

1 个答案:

答案 0 :(得分:0)

这可能会解决问题。

<?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";
}
?>