【发布时间】:2017-09-21 15:25:55
【问题描述】:
我使用表单上传图片。 我有两张表,一张是product_profile,另一张是product_image。
在我的表单中,我有 2 个按钮。一个上传product_profile的图片,另一个上传product_image中的图片。
但现在我遇到了 if、else if 和 else 的问题。在“//检查所有字段不为空”之间
当我只为 product_image "else if(empty($imgFileProduct))" 插入文件而不为 profile_image "else if(empty($imgFile))" 插入文件时,这与消息“请选择图像文件”正常工作。
但是当我只插入 profile_image 时,他插入数据库并上传文件而不检查 product_image 是否为空,并且我有以下消息“对不起,只允许 JPG、JPEG、PNG 和 GIF 文件2。”而不是“请选择图像文件”。
感谢您的帮助,一整天都在这。
<?php
error_reporting( ~E_NOTICE );
require_once 'dbconfig.php';
if(isset($_POST['btnsave']))
{
$catalogname = $_POST['catalog_name'];
$catalogmaker = $_POST['catalog_maker'];
$catalogtypes = $_POST['catalog_types'];
$catalogscale = $_POST['catalog_scale'];
$catalogedition = $_POST['catalog_edition'];
$imgFile = $_FILES['profile_image']['name'];
$tmp_dir = $_FILES['profile_image']['tmp_name'];
$imgSize = $_FILES['profile_image']['size'];
$imgFileProduct = $_FILES['product_image']['name'];
$imgSizeProduct = $_FILES['product_image']['tmp_name'];
$tmp_dirProduct = $_FILES['product_image']['size'];
//Check all fields are not empty
if(empty($catalogname)){
$errMSG = "Please Enter Product Name.";
}
else if(empty($catalogmaker)){
$errMSG = "Please Enter a Maker.";
}
else if(empty($catalogtypes)){
$errMSG = "Please Enter a Types.";
}
else if(empty($catalogscale)){
$errMSG = "Please Enter a Scale.";
}
else if(empty($catalogedition)){
$errMSG = "Please Enter a Edition.";
}
else if(empty($imgFile)){
$errMSG = "Please Select Image File.";
}
else if(empty($imgFileProduct)){
$errMSG = "Please Select Image File(S).";
}
//Check all fields are not empty
else
{
$upload_dir = 'product_images/'; // upload directory
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
$userpic= rand(1000,1000000).".".$imgExt;
if(in_array($imgExt, $valid_extensions)){
if($imgSize < 5000000){
move_uploaded_file($tmp_dir,$upload_dir.$userpic);
}
else{
$errMSG = "Sorry, your file is too large.";
}
}
else{
$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed3.";
}
if(!isset($errMSG)){
$stmt = $DB_con->prepare('INSERT INTO id_catalog(name,maker,types,scale,edition,pic) VALUES(:uname, :umaker, :utypes, :uscale, :uedition, :upic)');
$stmt->bindParam(':uname',$catalogname);
$stmt->bindParam(':umaker',$catalogmaker);
$stmt->bindParam(':utypes',$catalogtypes);
$stmt->bindParam(':uscale',$catalogscale);
$stmt->bindParam(':uedition',$catalogedition);
$stmt->bindParam(':upic',$userpic);
if($stmt->execute()){
$successMSG = "new record succesfully inserted ...";
header("refresh:5;index.php"); // redirects image view page after 5 seconds.
$last_id = $DB_con->lastInsertId();
echo "New record created successfully. Last inserted ID is: " . $last_id;
}
else{
$errMSG = "error while inserting....";
}
}
{
foreach($_FILES['product_image']['tmp_name'] as $key => $tmp_dirProduct ){
$imgFileProduct = $key.$_FILES['product_image']['name'][$key];
$imgSizeProduct =$_FILES['product_image']['size'][$key];
$tmp_dirProduct =$_FILES['product_image']['tmp_name'][$key];
$upload_dirProduct = 'product_images/';
$imgExtProduct = strtolower(pathinfo($imgFileProduct,PATHINFO_EXTENSION));
$valid_extensionsProduct = array('jpeg', 'jpg', 'png', 'gif');
$productpic= rand(1000,1000000).".".$imgExtProduct;
{
if(in_array($imgExtProduct, $valid_extensionsProduct)){
if($imgSizeProduct < 5000000){
move_uploaded_file($tmp_dirProduct,$upload_dirProduct.$productpic);
}
else{
$errMSG = "Sorry, your file is too large.";
}
}
else{
$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed2.";
}
if(!isset($errMSG)){
$stmt1 = $DB_con->prepare('INSERT INTO id_images(name_pic) VALUES(:uproductpic)');
$stmt1->bindParam(':uproductpic',$productpic);
if($stmt1->execute()){
$successMSG = "new record succesfully inserted ...";
header("refresh:5;index.php"); // redirects image view page after 5 seconds.
$last_id = $DB_con->lastInsertId();
echo "New record created successfully. Last inserted ID is: " . $last_id;
}
else{
$errMSG = "error while inserting....";
}
}
}
}
}
}
}
?>
【问题讨论】:
-
请删除与问题无关的代码部分。您已经提交了大约 150 行代码。
-
对不起@OptimusCrime。我编辑并删除了 html。
-
您的 $_FILES 是一个数组,因此您必须遍历它们并分别插入它们。当您从输入中删除多个和 [] 时,您不会发送数组,因此它可以工作。
-
向我们展示您尝试使用循环解决此问题,并描述您遇到的问题。 (如果您不清楚在这种情况下 $_FILES 将具有什么结构,请使用 var_dump 进行检查。)
-
@CBroe,我尝试使用循环。但我不是编码员。在可能尝试创建循环时,有时只上传一个文件而不插入数据库,或者全部上传但只有一行插入数据库。那是我的问题。我看到很多上传一个或多个文件的教程。但是没有人插入一个并且在多次上传之后。如果有人有这样的脚本,我会使用它....
标签: php mysql multi-upload