将值保存在数组中并在表中使用后出现问题

我在实习中做的练习涉及一个上传页面,一个表格页面,它将输出数据库表的记录和一个数据库表。问题是,经过巨大的努力使所有连接良好,我遇到了一些麻烦,因为 index.php 页面应该发送信息并且正在发送数组并保存数组而不是文件。下面是 index.php 页面的 PHP 代码:


 <?php

if (!isset($_POST['Enviar'])):  // This if will search if the $_POST variable already has anything 

or not.

$allowed =  array('txt');   //This array contains the file types which are allowed.

$uploadtime = time();

$uploadfile = $_FILES['file_to_upload'];

include 'db_connection.php';

$sql="select * from uploads";

$result = $conn->query($sql);


$target_dir = "uploads/";   //variable that will save the name of the folder or way where the file 

will be saved.

$target_file = $target_dir . basename($_FILES['file_to_upload']['name']); //NOTICE undefined index: 

file_to_upload... because it doesn't have any value safe there yet.

$goon = 1;  //This variable is used to check if there is any error messages or if the program can go 

on.

$ext = pathinfo( $_FILES["file_to_upload"]["name"], PATHINFO_EXTENSION); // This will use the OS to 

get the file extension.

if(!in_array($ext,$allowed) ) {

echo 'Erro: Ficheiro não permitido. <br> Por favor, verifique se o formato do ficheiro é txt.';

$goon = 0;

}   

第二段代码中的字段名称是数据库中的名称。谁能告诉我在 index.php 页面中收到此错误消息有什么问题:注意:第 66 行 C:\xampp\htdocs\16082019\index.php 中的数组到字符串转换


手掌心
浏览 145回答 2
2回答

撒科打诨

1st:谢谢你试图帮助我xD。第二:我已经有了一个适合我的练习的解决方案:$uploadfile = $_FILES['file_to_upload'];&nbsp;$uploadfile2 = $uploadfile['name'];&nbsp;然后用旧线代替:VALUES('".$_POST['file_name']."','$uploadfile2','$ext','$uploadtime')";

ABOUTYOU

“注意:数组到字符串的转换”表示您正在尝试将数组用作字符串。这是因为您将数组$_POST['file_name']与您的 INPUT 查询字符串连接起来。您可以使用implode()函数将数组转换为字符串:$values = implode(',', $_POST['file_name']);$sql = "INSERT INTO uploads (name,file_name,file_extension,uploaded_time)&nbsp; &nbsp; &nbsp; &nbsp; VALUES('" . $values . "','$uploadfile','$ext','$uploadtime')";
打开App,查看更多内容
随时随地看视频慕课网APP