我正在建立一个电子商务网站,我想上传多个可用于展示产品的图片。我的问题是它只上传了一个存储在我指导的文件中的图像' product_images' to和它的详细信息存储在db中。只有一个。我希望能够上传至少4.我怎么能这样做?
代码: FORM:
<form method="post" action="" enctype="multipart/form-data">
<div class="col-lg-6">
<p>PRODUCT DETAILS:</p> <br />
<tr>
<div class="form-group">
<label for="position">Enter Product Name:</label>
<div class="input-group">
<input type="text" class="form-control" name="prod_name" id="prod_name" placeholder="Enter Product Name:" required>
<span class="input-group-addon"><span class="glyphicon glyphicon-asterisk"></span></span>
</div>
</div>
</tr>
<tr>
<div class="form-group">
<label for="position">Enter Product Brand:</label>
<td><select type="position" class="form-control" id="prod_brand" name="prod_brand">
<OPTION value="Select Model..."></OPTION>
<?php brand(); ?>
</select></td>
</div>
</tr>
<tr>
<div class="form-group">
<label for="position">Enter Product Model:</label>
<td><select type="position" class="form-control" id="prod_model" name="prod_model">
<OPTION value="Select Model..."></OPTION>
<?php query(); ?>
</select></td>
<?php close(); ?>
</div>
</tr>
<tr>
<div class="form-group">
<label for="position">Enter Product Price:</label>
<div class="input-group">
<input type="text" class="form-control" name="prod_price" id="prod_price" placeholder="Enter Product Price:" required>
<span class="input-group-addon"><span class="glyphicon glyphicon-asterisk"></span></span>
</div>
</div>
</tr>
<tr>
<div class="form-group">
<label for="position">Enter Product Description:</label>
<div class="input-group">
<textarea style="width: 500px; height: 251px;" type="text" class="form-control" name="prod_desc" id="prod_desc" placeholder="Enter Product Description:" cols="20" rows="5" ></textarea>
</div>
</div>
</tr>
<tr>
<div class="form-group">
<label for="position">Enter Product Quantity:</label>
<div class="input-group">
<input type="text" class="form-control" name="prod_qty" id="prod_qty" placeholder="Enter Product Quantity:" required>
<span class="input-group-addon"><span class="glyphicon glyphicon-asterisk"></span></span>
</div>
</div>
<div class="form-group"><tr>
<td><label for="position">Select Images:</label></td>
<td><input type="file" name="product_image[]" id="product_image" multiple=""></td>
</div>
<!--<div class="form-group">
<input type="file" id="file-1" class="file" multiple name="product_image[]">
</div>-->
</tr>
</tr>
<tr>
<br /><td><input class="btn btn-info" type='submit' name='submit' value='Add Product' /></td>
</tr>
</form>
插入产品:
如果(isset($ _ POST [&#39;提交&#39;])){
$prod_name = strtoupper($_POST['prod_name']);
$prod_brand = $_POST['prod_brand'];
$prod_cat = $_POST['prod_model'];
$prod_price = $_POST['prod_price'];
$prod_desc = $_POST['prod_desc'];
$prod_qty = $_POST['prod_qty'];
$d = date("Y-m-d");
$id_query = $connection->query("INSERT into products(product_cat, product_brand, product_title, product_price, product_desc, product_qty, date)
VALUES ('$prod_cat','$prod_brand','$prod_name','$prod_price','$prod_desc','$prod_qty','$d')");
$query = mysql_insert_id();
$rand = rand(5,9878967699);
foreach($_FILES['product_image']['tmp_name'] as $key => $tmp_name){
$name = $_FILES['product_image']['name'][$key];
$tmpname = $_FILES['product_image']['tmp_name'][$key];
$type = $_FILES['product_image']['type'][$key];
$size = $_FILES['product_image']['size'][$key];
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$rand_dir_name = substr(str_shuffle($chars), 0, 15); //will pick the random 15 first characters
mkdir("product_images/$rand_dir_name/");
$dir = "product_images/$rand_dir_name/";
$move = move_uploaded_file($tmpname, $dir.$name);
if($move) {
//$query = mysql_query("insert into products (product_cat, product_brand, product_title, product_price, product_desc, product_qty, product_image, date)
//VALUES('$prod_cat','$prod_brand','$prod_name','$prod_price','$prod_desc','$prod_qty','$rand_dir_name/$name',now())");
$query = $connection->query("update products set product_image='$rand_dir_name/$name' where product_title='$prod_name'");
if($query) {
echo "<script>alert('Adding a product is successful!!!.')</script>";
} else {
echo "<script>alert('Adding a product is unsuccessful!!!.')</script>";
}
} else {
echo 'Picture upload failed';
}
}
}
答案 0 :(得分:1)
您可以创建另一个表来存储图像链接,并通过其产品ID将其“连接”到表产品。
所以:
$products = mysqli_query($conn, "SELECT * FROM products");
if (mysqli_num_rows($products) > 0) {
while ($product = mysqli_fetch_assoc($products)) {
echo "<p>Images for ".$product['ProductName'].":<br>";
$images = mysqli_query($conn, "SELECT * FROM images WHERE ProductID = ".$product['ID']."");
if (mysqli_num_rows($images) > 0) {
while ($image = mysqli_fetch_assoc($images)) {
echo "- ".$image['ImageSrc']."<br>";
}
} else {
echo "No images";
}
echo "</p>";
}
}
然后选择所有产品。在while循环中,您可以按产品ID为每个产品选择所有相应的图像。
这样您就可以为每件产品存储无限量的图像。
如果您只想要4张图片,那么只需为图片链接制作4列
更新1:回复评论
首先,开始使用PDO或其他mysqli而不是mysql。
选择您喜欢的图片
map = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map))
.getMap();