Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.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 是否为一个产品上载和显示多个图像?_Php_Mysql - Fatal编程技术网

Php 是否为一个产品上载和显示多个图像?

Php 是否为一个产品上载和显示多个图像?,php,mysql,Php,Mysql,我正在使用PHP/MYSQL开发一个基本的电子商务网站。我只需要知道如何上传一个产品的多个图像,然后在产品页面中显示它们。 至于上传多个图像,我不想使用uploadify或类似的开源代码。我宁愿有3-4个额外的文件上传字段,如果可能的话 而且我无法集中精力显示图像(一个产品有多个图像)。我真的不明白该怎么做!因此,任何简单条款的建议都将不胜感激 目前我只能上传一个产品的图像 这是我到目前为止所做的,请忽略第一个文件中的mysql查询,因为在我将mysql转换为mysqli之前,这是一个尚未上线的

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

而且我无法集中精力显示图像(一个产品有多个图像)。我真的不明白该怎么做!因此,任何简单条款的建议都将不胜感激

目前我只能上传一个产品的图像

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

upload.php


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

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

要进行显示,您需要从photo db表中提取记录,或者使用
glob()
查找以主键+'.'开头的所有文件


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

此代码中没有任何内容试图在数据库中存储一个图像,更不用说多个图像了。@Jessica,你错得再错不过了。看看这个:
move_上传的_文件($_文件['fileField']['tmp_name'],“./inventory_图像/$newname”)@Jessica,图像存储在哪里?它们被存储在inventory_images文件夹中,并被重命名为fine,它们还被显示在产品页面fine上。如果我是你,我会用另一张桌子。。。product_文件(product_id,filename)并确保文件名是唯一的。@user2953877啊,不,技术上Jessica是正确的。“移动文件”和“将文件存储在数据库中”是有区别的。谢谢,我知道mysql函数已被弃用。这就是为什么我说请忽略它,因为我将在功能准备就绪后将其转换为mysqli!我真的不想在mysql数据库中保存图像,因为这不是一个好的做法!因此,请您能指导我如何根据我的代码将图像重命名为1_1.jpg,2_1.jpg吗?我不是说将图像存储在数据库中,我是说创建一个存储文件名和产品id的表。我现在就知道了。那么你认为哪一个最好呢?还有,我不明白的是这个。假设我在mysql中创建了一个存储文件名和产品id的表!现在让我们假设我想上传一个产品的多个图像,如何将产品名称保存在一个mysql列(文件名)中。我可以存储一个名称,但当有多个图像时会发生什么?我完全迷路了!
<?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();
}
?>
<?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>