Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP和MySQL向图库添加图像_Php_Mysql_Gallery - Fatal编程技术网

使用PHP和MySQL向图库添加图像

使用PHP和MySQL向图库添加图像,php,mysql,gallery,Php,Mysql,Gallery,我正在尝试为我的照片库创建一个后端,这样可以更方便地将图像上载到照片库 <li data-id="id-12" data-type="treeremoval"> <div class="column grid_3"> <a class="fancybox" rel="treeremoval" href="images/gallery/1.jpg" title="Tree Removal Lake of the Ozarks"> <img src="im

我正在尝试为我的照片库创建一个后端,这样可以更方便地将图像上载到照片库

<li data-id="id-12" data-type="treeremoval">
<div class="column grid_3">
<a class="fancybox" rel="treeremoval" href="images/gallery/1.jpg" title="Tree Removal Lake of  the Ozarks">
<img src="images/gallery/1.jpg" alt="Tree Removal Lake of the Ozarks" class="max-img-border"></a>
<h5>Lake of the Ozarks Tree Removal </h5>
</div>  
</li>



<li data-id="id-11" data-type="treetrimming">
<div class="column grid_3">
<a class="fancybox" rel="treetrimming" href="images/gallery/2.jpg" title="Osage Beach Tree Trimming">
<img src="images/gallery/2.jpg" alt="Osage Beach Tree Trimming" class="max-img-border"></a>
<h5>Lake of the Ozarks Tree Trimming</h5>
</div>  
</li>
这个图库由一个缩略图像、一行描述和一个精美的盒子连接而成。非常基本。(缩略图和花式方框图像使用相同的图像。)

我刚刚开始学习PHP和MySQL的基础知识,并想知道如何使用这些代码:(仅供参考,这只是图库的一部分。)

通过使用这种方法,我设置了它,所以我只需要处理缩略图和fancybox图像的一个图像

我希望我的要求有道理

我知道我所要求的可能有很多要解释,但任何帮助都将不胜感激。 即使是一些好教程的链接也可能有所帮助。我在谷歌上搜索了一两天,但找不到我要找的东西

此外,如果你看画廊,在分类中随意翻阅,你会发现在过渡中有一个很大的飞跃。我目前正在努力解决这个问题

如果你需要更多的信息,请让我知道,我会非常愿意提供它


谢谢大家!

您需要一个MySQL表,其中包含有关图像的信息和图像的文件名:

CREATE TABLE images (
    id int(3) not null auto_increment,
    data_type varchar(128) not null,
    title varchar(256) not null,
    file_name varchar(64) not null,
    primary key(id)
)
你需要制作一个图像上传表单,类似这样:

.max-img-border { 
 width:100%;
 height:auto;
 border:solid 2px #FFFFFF;
 margin-bottom:10px;
 }
<form enctype="multipart/form-data" action="uploader.php" method="POST">
    Data type: <input type="text" name="dataType"><br>
    Title: <input type="text" name="title"><br>
    Image to upload: <input type="file" name="image"><br>
    <input type="submit" value="Upload">
</form>
现在,在gallery页面上,您需要循环浏览数据库中的图像

<?php
$images = mysql_query("SELECT * FROM images");
while ($image=mysql_fetch_assoc($images))
{
    ?>
    <li data-id="id-<?=$image["id"] ?>" data-type="<?=$image["data_type"] ?>">
    <div class="column grid_3">
    <a class="fancybox" rel="<?=$image["data_type"] ?>" href="images/gallery/<?=$image["file_name"] ?>" title="<?=$image["title"] ?>">
    <img src="images/gallery/<?=$image["file_name"] ?>" alt="<?=$image["title"] ?>" class="max-img-border"></a>
    <h5><?=$image["title"] ?></h5>
    </div>  
    </li>
    <?php
}
?>


杰斯布斯-非常感谢你!我几乎理解了你所说的一切,我唯一的问题是关于主键(id)?我应该在表中的什么位置添加它,当你说NOTNULL时,我会让null框不勾选,对吗?是的,你只是让null框不勾选。在PHPMyAdmin中,应该有一个键下拉列表,您可以在其中选择主键文件上载正在工作,我收到消息说它已上载到库中,但当我检查库时,我收到以下错误:警告:mysql\u fetch\u assoc()预期参数1为resource,第4行C:\xampp\htdocs\bgtree.com\maingallery.php中给出的布尔值有什么想法吗?再次感谢您的帮助。请尝试
echo mysql_error()操作。更不用说我计算出来了。=)<代码>
$allowed_extensions = array("jpg","jpeg","png","gif");
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
if (!in_array($extension,$allowed_extensions))
{
    die("Only these file types are allowed: jpg, png, gif");
}
<?php
$images = mysql_query("SELECT * FROM images");
while ($image=mysql_fetch_assoc($images))
{
    ?>
    <li data-id="id-<?=$image["id"] ?>" data-type="<?=$image["data_type"] ?>">
    <div class="column grid_3">
    <a class="fancybox" rel="<?=$image["data_type"] ?>" href="images/gallery/<?=$image["file_name"] ?>" title="<?=$image["title"] ?>">
    <img src="images/gallery/<?=$image["file_name"] ?>" alt="<?=$image["title"] ?>" class="max-img-border"></a>
    <h5><?=$image["title"] ?></h5>
    </div>  
    </li>
    <?php
}
?>