为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
手掌心 2022-01-24 10:02:47
我在实习中做的练习涉及一个上传页面,一个表格页面,它将输出数据库表的记录和一个数据库表。问题是,经过巨大的努力使所有连接良好,我遇到了一些麻烦,因为 index.php 页面应该发送信息并且正在发送数组并保存数组而不是文件。下面是 index.php 页面的 PHP 代码: <?phpif (!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 中的数组到字符串转换
查看完整描述

2 回答

?
撒科打诨

TA贡献1934条经验 获得超2个赞

1st:谢谢你试图帮助我xD。


第二:我已经有了一个适合我的练习的解决方案:


$uploadfile = $_FILES['file_to_upload']; 

$uploadfile2 = $uploadfile['name']; 

然后用旧线代替:


VALUES('".$_POST['file_name']."','$uploadfile2','$ext','$uploadtime')";


查看完整回答
反对 回复 2022-01-24
?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

“注意:数组到字符串的转换”表示您正在尝试将数组用作字符串。这是因为您将数组$_POST['file_name']与您的 INPUT 查询字符串连接起来。您可以使用implode()函数将数组转换为字符串:


$values = implode(',', $_POST['file_name']);

$sql = "INSERT INTO uploads (name,file_name,file_extension,uploaded_time)

        VALUES('" . $values . "','$uploadfile','$ext','$uploadtime')";


查看完整回答
反对 回复 2022-01-24
  • 2 回答
  • 0 关注
  • 144 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信