【发布时间】: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"> </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"> </td>
<td><?php echo $details; ?></td>
</tr>
<tr>
<td height="110"> </td>
<td style="font-family:Times New Roman; font-size:1.8em;">Price: £<?php echo $price; ?></td>
</tr>
<tr>
<td height="50"> </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 啊,从技术上讲,杰西卡是正确的。 “移动文件”和“将文件存储在数据库中”是有区别的。