多个图像上传并保存到数据库

时间:2018-06-07 21:33:06

标签: php

再次回来!所以我试图将多个图像上传到服务器并将路径和相应的库存号保存在数据库中。目前我有汽车细节,$ sqlcarinsert,工作和插入相关数据库,forsale。

当我尝试保存所有图像细节时,会出现问题。我有图像被赋予新名称并上传到服务器但没有输入到具有stockID的数据库,stockID将其链接到广告以及文件路径。除此之外,只有一个文件被输入到数据库中,而不是所有三个文件都被上传到服务器。这可能吗?

需要其他任何东西让我知道我会尽力提供!

P.S是的我知道准备好的陈述,杂乱的代码等等。

<?php

if(isset($_POST['add'])){

include '../Login-System/db.php';

$make = mysqli_real_escape_string($conn, $_POST['Make']);
$model = mysqli_real_escape_string($conn, $_POST['Model']);
$variant = mysqli_real_escape_string($conn, $_POST['Variant']);
$year = mysqli_real_escape_string($conn, $_POST['Year']);
$mileage = mysqli_real_escape_string($conn, $_POST['Mileage']);
$fuel = mysqli_real_escape_string($conn, $_POST['Fuel']);
$doors = mysqli_real_escape_string($conn, $_POST['Doors']);
$trans = mysqli_real_escape_string($conn, $_POST['transmission']);
$enginesize = mysqli_real_escape_string($conn, $_POST['Enginesize']);
$price = mysqli_real_escape_string($conn, $_POST['Price']);
$description = mysqli_real_escape_string($conn, $_POST['description']);

$sqlcarinsert = "INSERT INTO forsale (make, model, variant, year, mileage, fuel, doors, trans, enginesize, price, description) VALUES ('$make','$model','$variant','$year','$mileage','$fuel','$doors','$trans','$enginesize','$price','$description');";


//Image Upload

//Find next StockID (Surely not, right?)
$sql = "SELECT * FROM forsale ;";
$result = mysqli_query($conn, $sql);
$rowcount = mysqli_num_rows($result);   
$stockID = $rowcount + 1;



if(!empty($_FILES['files']['name'][0])){

    $files = $_FILES['files'];

    //File Extensions allowed
    $allowed = array('jpg', 'jpeg', 'png');

    foreach ($files['name'] as $position => $file_name) {
        $file_tmp = $files['tmp_name'] [$position];
        $file_size = $files['size'] [$position];
        $file_error = $files['error'] [$position];

        //Get file extension
        $FileExt = explode('.', $file_name);
        $endext = end($FileExt);
        $fileActualExt = strtolower($endext);

        if (in_array($fileActualExt, $allowed)) {
        //Checks for Errors in uploading
            if ($file_error === 0) {

                include '../Login-System/db.php';

                //New name to remove possibilities of duplicates
                $fileNameNew = uniqid('', true).".".$fileActualExt ;
                $FileDestination = '../Photos/forsale/'.$fileNameNew;
                //Upload to Designated folder with name
                move_uploaded_file($file_tmp, $FileDestination);
                //Insert into forsaleimg
                $sqlimginsert = "INSERT INTO forsaleimg (StockID, FileDestination) VALUES ('$stockID','$FileDestination');";

                mysqli_query($conn, $sqlimginsert);




            } else {
                header("Location: ../salelist.php?upload=error");
                exit();
            }
        }
    }   
}

mysqli_query($conn, $sqlcarinsert);
header("Location: ../salelist.php?added=".$make);
exit();


} else {
header("Location: ../salelist.php?add=notlcicked");
exit();
}

0 个答案:

没有答案