HTML表单中的php dropzone-将textarea值提交到单独的数据库表
我有一个表单,可以使用dropzone上传多个文件——我有这个功能,并将值发布到数据库中。在这个表单中,我有一个文本区域,我想上传到同一个数据库,但不同的表。为了便于参考,图像上载到名为files的表中,该表有一个名为post_id的列,该列将引用posts表中的id列。以下是表格:HTML表单中的php dropzone-将textarea值提交到单独的数据库表,php,upload,dropzone.js,Php,Upload,Dropzone.js,我有一个表单,可以使用dropzone上传多个文件——我有这个功能,并将值发布到数据库中。在这个表单中,我有一个文本区域,我想上传到同一个数据库,但不同的表。为了便于参考,图像上载到名为files的表中,该表有一个名为post_id的列,该列将引用posts表中的id列。以下是表格: <form action="file_upload.php" method="POST" enctype="multipart/form-data"
<form action="file_upload.php" method="POST" enctype="multipart/form-data">
<textarea class="form-control border-bottom" name="gallery_text" id="gallery_text" placeholder="Add gallery message..."></textarea>
<div class="dropzone mt-3" id="myDropzone"></div>
<button class="btn btn-primary mt-3" type="submit" name="gallery_submit" id="gallery_submit"> Post Gallery </button>
</form>
我想我可以使用isset
获取表单textarea
值并将其上传到相应的表中,然后使用lastInsertId上传图像并引用帖子。目前,我似乎根本无法获取要上传到数据库的textarea
值。因为我在这方面相对较新,所以我很难理解我错在哪里。我觉得我错过了什么。这是我尝试过的方法之一
if (isset($_POST['submit'])) {
$gallery_text = $_POST['gallery_text'];
$userLoggedIn = $user['username'];
$text_sql = "INSERT INTO posts(body, added_by, user_to, date_added, user_closed, deleted, likes, image) VALUES(?,?,'',NOW(),'no','no',0,'')";
$text_stmt = $pdo->prepare($text_sql);
$text_stmt->execute([$gallery_text, $userLoggedIn]);
}
如果您能给我一些指导,我将不胜感激
编辑:
我把表格分离出来,只需提交“图库文本”就行了。当我添加dropzone时,图像上传过程会悄无声息地失败。还在寻找。以下是有效的表格:
if (isset($_POST['gallery_submit'])) {
$gallery_text = $_POST['gallery_text'];
$userLoggedIn = "steve_shead";
$date_added = date("Y-m-d H:i:s");
$text_sql = "INSERT INTO posts(body, added_by, user_to, date_added, user_closed, deleted, likes, image) VALUES(?,?,?,?,?,?,?,?)";
$text_stmt = $con->prepare($text_sql);
$text_stmt->execute([$gallery_text, $userLoggedIn,"", $date_added, "no", "no", 0, ""]);
echo "<h1 class='display-1 mt-5 text-center'>Success!</h1>";
}
if(isset($\u POST['gallery\u submit'])){
$gallery\u text=$\u POST['gallery\u text'];
$userLoggedIn=“steve_shead”;
$date_added=日期(“Y-m-d H:i:s”);
$text\u sql=“插入帖子(正文、添加人、用户、添加日期、用户、关闭、删除、喜欢、图像)值(?,,,,,,,,?)”;
$text\u stmt=$con->prepare($text\u sql);
$text\u stmt->execute([$gallery\u text,$userLoggedIn,“,$date\u添加了“否”、“否”、0、”);
呼应“成功!”;
}
重新阅读文档后,我现在就可以使用它了。形式保持不变。以下是表单处理程序文件_upload.php:
<?php
require 'inc/header.php';
$gallery_text = $_POST['gallery_text'];
$userLoggedIn = $user['username'];
$date_added = date("Y-m-d H:i:s");
$text_sql = "INSERT INTO posts(body, added_by, user_to, date_added, user_closed, deleted, likes, image) VALUES(?,?,?,?,?,?,?,?)";
$text_stmt = $pdo->prepare($text_sql);
$text_stmt->execute([$gallery_text, $userLoggedIn, "", $date_added, "no", "no", 0, ""]);
$post_id = $pdo->lastInsertId();
// Count # of uploaded files in array
$total = count($_FILES['file']['name']);
$filename_arr = [];
// Loop through each file
for ($i = 0; $i < $total; $i++) {
// Get the temp file path
$tmpFilePath = $_FILES['file']['tmp_name'][$i];
// Make sure we have a file path
if ($tmpFilePath != "") {
// Setup our new file path
$newFilePath = "assets/img/posts/" . $_FILES['file']['name'][$i];
// Upload the file into the temp dir
if (move_uploaded_file($tmpFilePath, $newFilePath)) {
$filename_arr[] = $newFilePath;
$images_sql = "INSERT INTO files (file_name, uploaded_on, post_id) VALUES(?,?,?)";
$images_stmt = $pdo->prepare($images_sql);
$images_stmt->execute([$newFilePath, $date_added, $post_id]);
}
}
}
?>
<?php include 'inc/footer.php'; ?>
另请注意,PDO函数lastInsertId()
-我分配了变量$post\u id=$PDO->lastInsertId
-这样文本链接到上传的图像。在创建images数据库表时,我添加了一个名为post_id的INT行,通过使用lastInserId,它将图像与post关联起来(希望有意义?)
如果有人有类似的问题,如果你认为有帮助的话,我很乐意扩展解决方案
if (isset($_POST['gallery_submit'])) {
$gallery_text = $_POST['gallery_text'];
$userLoggedIn = "steve_shead";
$date_added = date("Y-m-d H:i:s");
$text_sql = "INSERT INTO posts(body, added_by, user_to, date_added, user_closed, deleted, likes, image) VALUES(?,?,?,?,?,?,?,?)";
$text_stmt = $con->prepare($text_sql);
$text_stmt->execute([$gallery_text, $userLoggedIn,"", $date_added, "no", "no", 0, ""]);
echo "<h1 class='display-1 mt-5 text-center'>Success!</h1>";
}
<?php
require 'inc/header.php';
$gallery_text = $_POST['gallery_text'];
$userLoggedIn = $user['username'];
$date_added = date("Y-m-d H:i:s");
$text_sql = "INSERT INTO posts(body, added_by, user_to, date_added, user_closed, deleted, likes, image) VALUES(?,?,?,?,?,?,?,?)";
$text_stmt = $pdo->prepare($text_sql);
$text_stmt->execute([$gallery_text, $userLoggedIn, "", $date_added, "no", "no", 0, ""]);
$post_id = $pdo->lastInsertId();
// Count # of uploaded files in array
$total = count($_FILES['file']['name']);
$filename_arr = [];
// Loop through each file
for ($i = 0; $i < $total; $i++) {
// Get the temp file path
$tmpFilePath = $_FILES['file']['tmp_name'][$i];
// Make sure we have a file path
if ($tmpFilePath != "") {
// Setup our new file path
$newFilePath = "assets/img/posts/" . $_FILES['file']['name'][$i];
// Upload the file into the temp dir
if (move_uploaded_file($tmpFilePath, $newFilePath)) {
$filename_arr[] = $newFilePath;
$images_sql = "INSERT INTO files (file_name, uploaded_on, post_id) VALUES(?,?,?)";
$images_stmt = $pdo->prepare($images_sql);
$images_stmt->execute([$newFilePath, $date_added, $post_id]);
}
}
}
?>
<?php include 'inc/footer.php'; ?>
<script>
Dropzone.options.myDropzone= {
url: "file_upload.php",
autoProcessQueue: false,
uploadMultiple: true,
parallelUploads: 5,
maxFiles: 20,
maxFilesize: 2,
acceptedFiles: ".jpeg,.jpg,.png,.gif",
addRemoveLinks: true,
dictDuplicateFile: "Duplicate Files Cannot Be Uploaded",
preventDuplicates: true,
init: function() {
var myDropzone = this; // Makes sure that 'this' is understood inside the functions below.
// For Dropzone to process the queue (instead of default form behavior):
document.getElementById("gallery_submit").addEventListener("click", function(e) {
// Make sure that the form isn't actually being sent.
e.preventDefault();
e.stopPropagation();
myDropzone.processQueue();
});
// Send all the form data along with the files:
this.on("sendingmultiple", function(data, xhr, formData) {
formData.append("gallery_text", jQuery("#gallery_text").val());
});
}
}