【问题标题】:projid is not properly matching foreign keyprojid 没有正确匹配外键
【发布时间】:2016-03-23 13:23:57
【问题描述】:

最近我创建了一个表单,该表单连接到 2 个表,其中 1 个包含文件,其他包含项目,同时提交表单时您可以上传文件以及 1 个以上,因为我使用的上传器是 dropzone.js,并且 ebveryone 知道 dropzone 提交当有人上传文件并且不提交表单时,文件具有不同的表单 id 冲突假设我上传文件,但后来我决定不提交表单 这里发生的情况是,当下一个用户来填写并提交时,文件将保存在数据库中表格连同上传文件 这里发生的是分配给以前文件的 id 将与当前文件匹配 我相信通过查看代码你会明白我做了什么和我需要什么 我不知道如何解释但有时提交的文件包含错误的项目

dropzone上传代码

<?php 
require_once("../../../includes/connection.php");

if(!empty($_FILES)){

    $targetDir = "../../uploads/";
    $fileName = $_FILES['file']['name'];
    $targetFile = $targetDir.$fileName;

    //Getting ID to match with the submitted project form details
    $query_get = mysqli_query($connection, "SELECT * FROM projects ORDER BY proj_id DESC LIMIT 1");
    while($id = mysqli_fetch_assoc($query_get)) {
        $uid = $id["proj_id"];
    }
    $uid1 = $uid+1;//This id will becomes imaginary now as if the files and form were submitting at a time the i would have used mysqli_insert_id but in this case i cannot use

    if(move_uploaded_file($_FILES['file']['tmp_name'],$targetFile)){
        //insert file information into db table
        $conn = mysqli_query($connection, "INSERT INTO files (proj_id, file_name, uploaded) VALUES('$uid1', '".$fileName."','".date("Y-m-d H:i:s")."')");
    }

}

?>

这是我的 startproject 表单提交功能

function add_project($connect) {
    $fname       = $_POST["first_name"];
    $lname       = $_POST["last_name"];
    $proj_name   = $_POST["proj_name"];
    $email       = $_POST["email"];
    $end_date    = $_POST["end_date"];
    $skype       = $_POST["skype"];
    $company_url = $_POST["url"];
    $detail1     = mysqli_real_escape_string($connect, $_POST["details1"]);
    $detail2     = mysqli_real_escape_string($connect, $_POST["details2"]);
    $detail3     = mysqli_real_escape_string($connect, $_POST["details3"]);
    $upload_date = date("F-d-Y");
    $query2      = "INSERT INTO projects (first_name, last_name, proj_name, end_date, detail1, detail2, detail3, email, uploaded_date, skype_name, company_url, status) VALUES ('$fname', '$lname', '$proj_name', '$end_date', '$detail1', '$detail2', '$detail3', '$email', '$upload_date', '$skype', '$company_url', 'submit')";
    $confirm2    = mysqli_query($connect, $query2);
}

谢谢,我相信这是一个常见的问题,但我希望有人能理解并帮助我

【问题讨论】:

    标签: php forms mysqli


    【解决方案1】:

    有两种方法可以解决您的问题:

    1. 从第一个请求 (dropzone.js) 返回 ID 并将其包含在第二个请求中(表单提交)
    2. 更改您的配置以在表单提交时上传文件(带有表单数据),因此只需要一次提交

    选项 1 - 您可以监听 dropzone .on("success") 事件并将返回的 ID 分配给表单数据,以便第二次提交以将它们关联在一起。

    Dropzone.options.myDropzone = {
      init: function() {
            myDropzone.on("success", function(file, responseText) {
                console.log(responseText);
                // assign the associating data
            });
        }
    };
    

    如果您的 php 脚本处理 dropzone 上传,您将输出 ID

    if(move_uploaded_file($_FILES['file']['tmp_name'],$targetFile)){
        //insert file information into db table
        $conn = mysqli_query($connection, "INSERT INTO files (proj_id, file_name, uploaded) VALUES('$uid1', '".$fileName."','".date("Y-m-d H:i:s")."')");
        // output the id
        if ($conn) echo $uid1;
    }
    

    注意:最好插入projects 表以获取新的projid,而不是假设上传将是下一个id,然后在第二次提交时更新projid。这是因为另一个用户可能同时上传并与第二次提交重叠。

    选项 2 - 可以使用 dropzone 上传表单数据。您需要在 dropzone 表单中添加表单元素。

    <form id="my-dropzone" class="dropzone">
      <div class="dropzone-previews"></div>
      <!-- place your form fields here -->
      <button type="submit">Submit data and files!</button>
    </form>
    

    在 dropzone 配置中设置 autoProcessQueue: false,然后在提交表单时处理队列。

    // First change the button to actually tell Dropzone to process the queue.
    this.element.querySelector("button[type=submit]").addEventListener("click", function(e) {
      // Make sure that the form isn't actually being sent.
      e.preventDefault();
      e.stopPropagation();
      myDropzone.processQueue();
    });
    

    您可能还想利用其他活动:

    // Listen to the sendingmultiple event. In this case, it's the sendingmultiple event instead
    // of the sending event because uploadMultiple is set to true.
    this.on("sendingmultiple", function() {
      // Gets triggered when the form is actually being sent.
      // Hide the success button or the complete form.
    });
    this.on("successmultiple", function(files, response) {
      // Gets triggered when the files have successfully been sent.
      // Redirect user or notify of success.
    });
    this.on("errormultiple", function(files, response) {
      // Gets triggered when there was an error sending the files.
      // Maybe show form again, and notify user of error
    });
    

    【讨论】:

    • 非常感谢您的选择,我将进程设置为 false,并将 this.element.querySelector 函数放在表单下方,但我在 colsole 日志中收到此错误 Uncaught TypeError: Cannot read property 'querySelector' of未定义 |做错了什么?
    猜你喜欢
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 2016-07-03
    • 2011-06-05
    • 1970-01-01
    相关资源
    最近更新 更多