【问题标题】:upload and display multiple images for one product?为一个产品上传并显示多张图片?
【发布时间】:2014-01-03 20:23:54
【问题描述】:

我正在使用 PHP/MYSQL 开发一个基本的电子商务网站。我只需要知道如何为一个产品上传多张图片,然后在产品页面中显示它们。 至于上传多张图片,我不想使用uploadify或类似的开源代码。如果可能的话,我宁愿有 3-4 个额外的文件上传字段!

而且我无法理解显示图像(1 个产品的多个图像)。我真的不明白它应该如何工作!所以任何关于简单术语的建议都将不胜感激。

目前我只能为每个产品上传 1 张图片。

这是我到目前为止的内容,请忽略第一个文件中的 mysql 查询,因为在我将 mysql 转换为 mysqli 之前,这还没有上线。只需要先对函数进行排序:

上传.php

<?php 
// Parse the form data and add inventory item to the system
if (isset($_POST['product_name'])) {

    $product_name = mysql_real_escape_string($_POST['product_name']);
    $price = mysql_real_escape_string($_POST['price']);
        $quantity = mysql_real_escape_string($_POST['quantity']);
    $category = mysql_real_escape_string($_POST['category']);
    $details = mysql_real_escape_string($_POST['details']);
    // See if that product name is an identical match to another product in the system
    $sql = mysql_query("SELECT id FROM products WHERE product_name='$product_name' LIMIT 1");
    $productMatch = mysql_num_rows($sql); // count the output amount
    if ($productMatch > 0) {
        echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="add.php">click here</a>';
        exit();
    }
    // Add this product into the database now
    $sql = mysql_query("INSERT INTO products (product_name, price, quantity, details, category, date_added) 
        VALUES('$product_name','$price','$quantity','$details','$category',now())") or die (mysql_error());
     $pid = mysql_insert_id();
    // Place image in the folder 
    $newname = "$pid.jpg";
    move_uploaded_file( $_FILES['fileField']['tmp_name'], "../inventory_images/$newname");
    header("location: add.php"); 
    exit();
}
?>

product.php

<?php 
// Check to see the URL variable is set and that it exists in the database
if (isset($_GET['id'])) {
    // Connect to the MySQL database  
    include "config/connect.php"; 
    $id = preg_replace('#[^0-9]#i', '', $_GET['id']); 
    // Use this var to check to see if this ID exists, if yes then get the product 
    // details, if no then exit this script and give message why
    $sql = "SELECT * FROM products WHERE id='$id' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $productCount = mysqli_num_rows($query); // count the output amount
    if ($productCount > 0) {
        // get all the product details
            while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
             $product_name = $row["product_name"];
             $price = $row["price"];
             $details = $row["details"];
             $quantity = $row["quantity"];
             $category = $row["category"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
         }

    } else {
        echo "That item does not exist.";
        exit();
    }

} else {
    echo "Data to render this page is missing.";
    exit();
}

?>

<table width="900" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="300" rowspan="5" align="right" valign="top" style="padding-top:10px;"><img src="inventory_images/<?php echo $id; ?>.jpg" width="300" height="450" alt="<?php echo $product_name; ?>" /></td>
    <td width="126" height="106">&nbsp;</td>
    <td width="274"><h3 style="font-family:Times New Roman; font-size:1.8em;"><?php echo $product_name; ?></h3></td>
  </tr>
  <tr>
    <td height="120">&nbsp;</td>
    <td><?php echo $details; ?></td>
  </tr>
  <tr>
    <td height="110">&nbsp;</td>
    <td style="font-family:Times New Roman; font-size:1.8em;">Price: £<?php echo $price; ?></td>
  </tr>
    <tr>
    <td height="50">&nbsp;</td>
    <td style="font-family:Times New Roman; font-size:1.8em;">Quantity Left: <?php echo $quantity; ?></td>
  </tr>
</table>

谢谢

【问题讨论】:

  • 这段代码中没有任何东西试图在数据库中存储一张图像,更不用说多张了。
  • @Jessica,好吧,你大错特错了。看看这个:move_uploaded_file( $_FILES['fileField']['tmp_name'], "../inventory_images/$newname");
  • @Jessica,图片存储在哪里?它们被存储在inventory_images 文件夹中,它们被重命名并且它们也很好地显示在产品页面上。目前每种产品只有一个。
  • 如果我是你,我会使用另一个表... product_files (product_id, filename) 并确保文件名是唯一的。
  • @user2953877 啊,从技术上讲,杰西卡是正确的。 “移动文件”和“将文件存储在数据库中”是有区别的。

标签: php mysql


【解决方案1】:

好吧,您目前的做法并没有真正为多张照片设置,因为您没有在数据库中存储对照片的引用。您只是将图像重命名为产品的主键。因此,您需要执行 1_1.jpg 1_2.jpg 之类的操作,或者您需要创建一个存储文件名和产品 ID 的数据库表,这样您就可以建立一对多关系。

至于上传更多图片,只需在表单中添加更多文件输入即可。

为了显示,您需要从照片数据库表中提取记录或使用glob() 查找所有以主键 + '_' 开头的文件。

另外,仅供参考的 mysql 函数不应再使用,因为它们已被弃用。

【讨论】:

  • 谢谢,我知道 mysql 功能已被弃用。这就是为什么我说请忽略它,因为一旦功能准备好,我将把它转换为 mysqli!
  • 我真的不想将图像保存在 mysql 数据库中,因为这不是一个好习惯!那么请您指导我如何根据我的代码将图像重命名为 1_1.jpg、2_1.jpg?
  • 我不是说将图像存储在数据库中,而是说创建一个存储文件名和产品 ID 的表。
  • 我现在找到你了。那么你认为哪一个最好呢?
  • 另外,我不明白的是这个。假设我在 mysql 中创建了一个存储文件名和产品 ID 的表!现在假设我想为一种产品上传多张图片,如何将产品名称保存在一个 mysql 列(文件名)中。我可以存储 1 个名称,但是当有多个图像时会发生什么?我完全迷路了!
猜你喜欢
  • 2019-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-11
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多