gpt4 book ai didi

php - [PHP][MySQL] 如何在上传表单中为每个选定的文件实现一个标题字段?

转载 作者:行者123 更新时间:2023-11-29 02:11:15 26 4
gpt4 key购买 nike

我是一个 php/mysql 新手,需要你的帮助!
我有一个上传公式,可以将多个文件上传到数据库,一个一个地选择。
文件上传工作正常,但我想为每个选定的文件添加一个标题字段。我不知道如何在我的代码中实现它。
这是我的代码:

$db = mysqli_connect("localhost", "root", "", "xy");
if (isset($_POST['upload_docs'])) {

for($mi = 0; $mi < count($_FILES['sel_doc']['tmp_name']); $mi++) {

$doc = $_FILES['sel_doc']['name'][$mi];
$tmp_doc = $_FILES['sel_doc']['tmp_name'][$mi];
$target = "doc_uploads/". basename($doc);

if (move_uploaded_file($tmp_doc, $target)) {
echo "Upload successfully!";
}
else {
echo "Upload failed!";
}
$sql_f = "INSERT INTO docs (doc_file) VALUES ('$doc')";
mysqli_query($db, $sql_f);
}
}



<form method="POST" action="upload.php" enctype="multipart/form-data">
<div>
<input type="file" name="sel_doc[]">
<input type="file" name="sel_doc[]">
<input type="file" name="sel_doc[]">
<input type="file" name="sel_doc[]">
<input type="file" name="sel_doc[]">
</div>

<div id="upload_button">
<button type="submit" name="upload_docs">upload</button>
</div>
</form>

这是 1 个上传的样子:

|    id_doc  |  doc_file |  doc_title|upload_id|
++++++++++++++++++++++++++++++++++++++++++++++++
|.....1......|...a.pdf...|....vvv....|....1....|
|.....2......|...b.pdf...|....www....|....1....|
|.....3......|...c.pdf...|....xxx....|....1....|
|.....4......|...d.pdf...|....yyy....|....1....|
|.....5......|...e.pdf...|....zzz....|....1....|

在我的原始代码中,我在 2 个表中插入了 1:n 关系(因为“upload_id”)。

最佳答案

HTML

<div>
<?php for ($i=0; $i<5; $i++){ ?>
<b><?php echo $i ?></b><br>
File: <input type="file" name="sel_doc[<?php echo $i ?>]"><br>
Title: <input name="title[<?php echo $i ?>]"><br>
<?php } ?>
</div>

将打印这个:

<div>
<b>0</b><br>
File: <input type="file" name="sel_doc[0]"><br>
Title: <input name="title[0]"><br>
<b>1</b><br>
File: <input type="file" name="sel_doc[1]"><br>
Title: <input name="title[1]"><br>
<b>2</b><br>
File: <input type="file" name="sel_doc[2]"><br>
Title: <input name="title[2]"><br>
<b>3</b><br>
File: <input type="file" name="sel_doc[3]"><br>
Title: <input name="title[3]"><br>
<b>4</b><br>
File: <input type="file" name="sel_doc[4]"><br>
Title: <input name="title[4]"><br>
</div>

PHP代码

$db = mysqli_connect("localhost", "root", "", "xy");
if (isset($_POST['upload_docs'])) {

for($mi = 0; $mi < count($_FILES['sel_doc']['tmp_name']); $mi++) {

$doc = $_FILES['sel_doc']['name'][$mi];
$tmp_doc = $_FILES['sel_doc']['tmp_name'][$mi];
$target = "doc_uploads/". basename($doc);
$title = (isset($_POST['title'][$mi])?$_POST['title'][$mi]:"");

if (move_uploaded_file($tmp_doc, $target)) {
echo "Upload successfully!";
}
else {
echo "Upload failed!";
}
$sql_f = "INSERT INTO docs (doc_file, title) VALUES ('$doc', '$title')";
mysqli_query($db, $sql_f);
}
}

顺便说一句;此示例代码对于sql 注入(inject) 是不安全的。

关于php - [PHP][MySQL] 如何在上传表单中为每个选定的文件实现一个标题字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51892589/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com