dsfykqq3403 2019-07-12 12:54
浏览 67

如何在注册过程中将多个文件上传到不同的表格?

Good day everyone! I am using Dropzone Js to upload multiple files into a different table during registration. However I couldn't make the upload and registration to work at the same time. Most Dropzone tutorial code that I see online are using mysqli while I'm using PDO making it hard to integrate in to my code. I need to save the details of the user to a table while saving the files they uploaded to a different table together with the coopID as a foreign key.

I've tried using Dropzone Js to upload multiple files easily and I also use the php PDO function lastInsertID() to get the coopID of the user.

    <?php
include_once 'coop_functions.php';
session_start();

if(isset($_SESSION['loggedUser'])) {
    header("Location: coop_dashboard.php");
}


$errorList = NULL;
$firstName = "";
$lastName = "";
$coopName = "";
$userName = "";
$email = "";
$regNumber = "";
$coopNumber = "";
$address = "";
$contact = "";
$contact2 = "";
$city = "";
$status = "";
$coopType = "";
$photo = "";


// this code works but it doesn't save the coopID of the user
// if I put it inside if(isset($_POST['register'])) it doesn't work

if (! empty($_FILES)) {
    $imagePath = isset($_FILES["file"]["name"]) ? $_FILES["file"]["name"] : "Undefined";
    $targetPath = "uploads/";
    $imagePath = $targetPath . $imagePath;
    $tempFile = $_FILES['file']['tmp_name'];

    $targetFile = $targetPath . $_FILES['file']['name'];

    recordUpload($tempFile, $targetFile);
}
if (!empty($_GET["action"]) && $_GET["action"] == "save") {
    require_once ("dbconfig.php");

    print $sql = "INSERT INTO upload (photo) VALUES ('. $imagePath .')";
    mysqli_query($conn, $sql);
    $current_id = mysqli_insert_id($conn);
}


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


    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $coopName = $_POST['coopName'];
    $userName = $_POST['userName'];
    $email = $_POST['email'];
    $regNumber = $_POST['regNumber'];
    $coopNumber = $_POST['coopNumber'];
    $address = $_POST['address'];
    $contact = $_POST['contact'];
    $contact2 = $_POST['contact2'];
    $city = $_POST['city'];
    $status = $_POST['status'];
    $coopType = $_POST['coopType'];


$imagePath = isset($_FILES["file"]["name"]) ? $_FILES["file"]["name"] : "Undefined";
    $targetPath = "uploads/";
    $imagePath = $targetPath . $imagePath;
    $tempFile = $_FILES['file']['tmp_name'];

        $targetFile = $targetPath . $_FILES['file']['name'];


    $errorList = registerValidateInput($firstName, $lastName, $coopName, $userName, $email, $regNumber, $coopNumber,
        $address, $contact, $contact2, $city, $status, $coopType, $_POST['password'], $_POST['confirmPassword']);


    if($errorList == NULL)
    {
        $errorList = registerVerifyInput($_POST['userName'], $_POST['email']);
        if($errorList == NULL)
        {
            $errorList = recordUpload($tempFile, $targetFile);
            if($errorList == NULL)
            {
                register(2, $_POST['firstName'], $_POST['lastName'], $_POST['coopName'], $_POST['userName'], $_POST['email'], $_POST['regNumber'], $_POST['coopNumber'], $_POST['address'],
                    $_POST['contact'], $_POST['contact2'], $_POST['city'], $_POST['status'], $_POST['coopType'], $_POST['password'], $targetFile);
                header("Location: coop_dashboard.php");
            }

        }
    }
}

?>

//I deleted the input tags to make the code short

<form class="login-form form-all" action="coop_register.php" method="post">

    <button type="submit" name="register" class="green-btn green-btn-form btn-login">Create </button>
</form>
<form name="frmImage" action="coop_register.php?action=save" class="dropzone">
</form>

//These are my functions it is located in a different file called coop_functions.php

function connect()
{
    global $server, $database, $dbusername, $dbpassword;
    try {
        $connection = new PDO("mysql:host=$server; dbname=$database", $dbusername, $dbpassword);
        $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $connection;
    } catch (PDOException $exception) {
        echo "Error encountered: " . $exception->getMessage();
        return NULL;
    }
}

function recordUpload($tempFile, $targetFile)
{

   $errorList = array();
    try {
        move_uploaded_file($tempFile, $targetFile);
    } catch(Exception $exception) {
        array_push($errorList, "Error: " . $exception -> getMessage());
    }
    if (!empty($errorList)) {
        return $errorList;
    } else {
        return NULL;
    }
}

function register($roleID, $firstName, $lastName, $coopName, $userName,  $email, $regNumber, $coopNumber, $address, $contact, $contact2, $city,
                  $status, $coopType, $password, $photo)
{
    try
    {
        $connection = connect();
        $stmt= $connection->prepare("INSERT INTO cooperative(roleID, firstName, lastName, 
        coopName, userName, email, regNumber, coopNumber, address, contact, contact2, city, status, coopType, password)
        VALUES (:roleID,:firstName, :lastName, :coopName, :userName, :email, :regNumber, :coopNumber, :address,
        :contact, :contact2, :city, :status, :coopType, :password)");
        $stmt->bindParam(":roleID", $roleID);
        $stmt->bindParam(":firstName", $firstName);
        $stmt->bindParam("lastName", $lastName);
        $stmt->bindParam(":coopName", $coopName);
        $stmt->bindParam(":userName", $userName);
        $stmt->bindParam(":email", $email);
        $stmt->bindParam("regNumber", $regNumber);
        $stmt->bindParam("coopNumber", $coopNumber);
        $stmt->bindParam(":address", $address);
        $stmt->bindParam(":contact", $contact);
        $stmt->bindParam(":contact2", $contact2);
        $stmt->bindParam(":city", $city);
        $stmt->bindParam(":status", $status);
        $stmt->bindParam(":coopType", $coopType);
        $hashedPassword = hash("sha256", $password);
        $stmt->bindParam(":password", $hashedPassword);
        $stmt->execute();

        $coopID = $connection->lastInsertID();
        $statement = $connection->prepare("INSERT INTO upload(coopID, photo) VALUES (:coopID, :photo)");
        $statement->bindParam(":coopID", $coopID);
        $statement->bindParam(":photo", $photo);
        $statement->execute();

        $coopID = $connection->lastInsertID();
        $detail = "$firstName $lastName registered into the system.";
        recordAudit($connection, $coopID, 1, $detail);
        recordUpload($connection, $coopID);
    }
    catch (PDOException $exception)
    {
        die("Error: " . $exception->getMessage());
    }

}

There is no error message it's just not working the way intend it. After registering either the coopID is save to the database but no upload files or files are uploaded but no coopID. I am beginner in PHP any help is appreciated thank you.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?