gpt4 book ai didi

php - 为多个图像制作类似按钮

转载 作者:行者123 更新时间:2023-11-30 22:08:03 24 4
gpt4 key购买 nike

我是 php 和 MySql 的新手,正在尝试制作一个图片上传网站,供用户上传图片。我将所有图像存储在一个目录中,并将图像的路径存储在我的数据库中。

因为我不知道已经上传了多少张图片,所以我使用了一个 while 循环来显示它们。

现在,我需要为每个图像创建“赞”按钮。虽然我能够为每张图片创建点赞按钮,但我无法理解 php 如何知道 哪个 图片的点赞按钮已被按下以及点赞按钮 被按下,我应该如何增加该特定图像的“喜欢数量”值,不仅在数据库中,而且在网页本身上。

我知道我必须为此使用 php、MySql 和 Ajax。如果您能帮助我编写代码,那就太好了。多谢! :)

这是我到目前为止能够编写的代码(在代码中,我没有展示点赞按钮的创建,因为我对此完全困惑......代码只包含图像的显示..如果你能帮助我了解如何继续创建点赞按钮以及如何更新数据库并在不刷新整个页面的情况下在网页上打印点赞数量,那就太好了):

$sql= "SELECT * FROM imagestable ";
$result=mysqli_query($conn,$sql);
//printing all the images one by one
while($row=mysqli_fetch_assoc($result)){

$imagelocation=$row['imageDestination'];
$imagetitle=$row['imagetitle'];
$uploader=$row['uploader'];
echo '<h1>'.$imagetitle.'</h1>
<img src="'.$imagelocation.'" style="width:600px;height:100%;">
<h1>Uploaded by:'.$uploader.'</h1>
<br>
';
}

谢谢!

最佳答案

您的数据库中可能应该有一个 Id 列来唯一标识每个图像(它们通常是自动生成和递增的,您无需执行任何操作,尽管我已经很久没有使用 MySql 了)。

$sql= "SELECT * FROM imagestable ";
$result=mysqli_query($conn,$sql);
//printing all the images one by one
while($row=mysqli_fetch_assoc($result)){

$imagid = $row['imageid'];
$imagelocation=$row['imageDestination'];
$imagetitle=$row['imagetitle'];
$uploader=$row['uploader'];
echo '<h1>'.$imagetitle.'</h1>
<img src="'.$imagelocation.'" style="width:600px;height:100%;">
<h1>Uploaded by:'.$uploader.'</h1>
<br>
// Here you add the button with the id from the database to identify the image
// Here likeimage(id) is assumed to be a javascript function that somehow updates
// the server that someone clicked the like button (and possibly updates the html DOM
// to the new number of likes.
<input type="button" onclick="likeimage($imageid);" />
';
}

关于php - 为多个图像制作类似按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41018163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com