[英]php script upload only the first image to database
我有一個需要上傳一些圖片的表格,但是當我在上傳器中上傳圖片時,僅保存了第一張圖片。 我不知道問題出在哪里。 非常感謝您的時間和幫助。
這是將圖片添加到我的數據庫的PHP腳本
session_start();
include '../../include/config.php';
// Create random name
function generateRandomString($length = 25) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
// Upload product image
$productimg = generateRandomString();
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["pimage"]["name"]);
$extension = end($temp);
$productimg = $productimg . "." . $extension;
move_uploaded_file($_FILES["pimage"]["tmp_name"],
"../../images/products/" . $productimg);
// Add product image
$sqlImage = "INSERT INTO productimages (pid, pimage) VALUES (" . $_REQUEST['pid'] . ",'" . $productimg . "')";
try{
$queryimg = $db->prepare($sqlImage);
$queryimg->execute();
}
catch(PDOException $ecop) {
die($ecop->getMessage());
}
//Select new default image
$sqlNewImage = "SELECT * FROM productimages WHERE pid=" . $_REQUEST['pid'];
try{
$query = $db->query($sqlNewImage);
$query->setFetchMode(PDO::FETCH_ASSOC);
$row = $query->fetch();
if ($query->rowCount() > 0) {
$newIMG = $row['pimage'];
}else
{
$newIMG = "default.png";
}
}
catch(PDOException $e) {
die($e->getMessage());
}
$sqlUpdateImage = "UPDATE products SET pimage = '" . $newIMG . "' WHERE id = " . $_REQUEST['pid'];
try{
$query = $db->prepare($sqlUpdateImage);
$query->execute();
}
catch(PDOException $e) {
die($e->getMessage());
}
$myUrl = "Location: ../index.php?p=edit&pid=" . $_REQUEST['pid'];
header($myUrl);
這是html代碼,函數形式在哪里
<form action="functions/upload_image.php?pid=<?php echo $productid; ?>" method="post" class="form-horizontal" enctype="multipart/form-data" data-parsley-validate>
<div class="form-group">
<label for="cimage" class="control-label col-lg-4">Nahrát obrázek</label>
<div class="col-lg-8">
<input type="file" name="pimage" id="pimage" multiple="true"/>
</div>
</div>
<div class="form-group">
<center><button class="btn btn-lg btn-success" type="submit"><i class="fa fa-upload"></i> Nahrát</button></center>
</div>
</form>
給輸入一個數組樣式的名稱:
<input type="file" name="pimage" id="pimage[]" multiple="true"/>
然后, $_FILES['pimage']
所有條目都是數組,您可以循環將其全部插入。
$sqlImage = $db->prepare("INSERT INTO productimages (pid, pimage) VALUES (:pid, , :productimg)";
$sqlImage->bindParam(":pid", $_REQUEST['pid']);
$sqlImage->bindParam(":productimg", $productimg);
foreach ($_FILES['pimage']['name'] as $i => $name) {
$tmp_name = $_FILES['pimage']['tmp_name'][$i];
$temp = explode(".", $name);
$extension = end($temp);
$productimg = generateRandomString() . "." . $extension;
move_uploaded_file($temp_name, "../../images/products/" . $productimg);
try {
$sqlImage->execute();
} catch(PDOException $ecop) {
die($ecop->getMessage());
}
// .. similar code for other queries
}
還要注意使用bindParam()
防止SQL注入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.