php - 为一个产品上传和显示多张图片?

标签 php mysql

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

而且我无法理解显示图像(1 个产品的多个图像)。我真的不明白它应该如何工作!因此,我们将不胜感激任何简单的建议。

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

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

upload.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 <<< this is the page that displays the product details and image.

<?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>

谢谢

最佳答案

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

至于上传更多图片,只需将更多文件输入添加到您的表单即可。

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

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

关于php - 为一个产品上传和显示多张图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20912104/

相关文章:

php - 选择似乎不起作用

php - 如何从自定义插件访问 wordpress 表

php - 删除行后重置 auto_increment

php - Error Domain=NSCocoaErrorDomain Code=3840 "No value."比较两个值时。 PHP、JSON 和 Swift

php - mysql查询的问题

html - 哪种 PHP 模板引擎语法最适合使用?

php - 将来自其他表的 select id 与 insert on single 命令结合起来

java - 搜索查询性能?

PHP preg 替换白帽字符

php - AJAX 更改表行上的注释单击