Php 上载文件后更新数据库时出现问题
我正在尝试上载文件,然后将文件名插入数据库 下面是我的代码:Php 上载文件后更新数据库时出现问题,php,mysql,database,file-upload,Php,Mysql,Database,File Upload,我正在尝试上载文件,然后将文件名插入数据库 下面是我的代码: <?php //connecting to database session_start(); include('/applications/MAMP/htdocs/connect.php'); $u = $_SESSION['username']; if (!empty($_FILES)) { $file = $_FILES['Filedata']; $tempFile = $_FILES['Filedata']
<?php
//connecting to database
session_start();
include('/applications/MAMP/htdocs/connect.php');
$u = $_SESSION['username'];
if (!empty($_FILES)) {
$file = $_FILES['Filedata'];
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
// $fileTypes = str_replace('*.','',$_REQUEST['fileext']);
// $fileTypes = str_replace(';','|',$fileTypes);
// $typesArray = split('\|',$fileTypes);
// $fileParts = pathinfo($_FILES['Filedata']['name']);
// if (in_array($fileParts['extension'],$typesArray)) {
// Uncomment the following line if you want to make the directory if it doesn't exist
// mkdir(str_replace('//','/',$targetPath), 0755, true);
$n = $file['name'];
move_uploaded_file($tempFile,$targetFile);
echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
mysqli_select_db($connect,"people");
$i = "update users set filelocation = '$n' where sessionusername = '$u'";
$q = mysqli_query($connect,$i);
// } else {
// echo 'Invalid file type.';
// }
}
?>
您有$n=$file['name']代码>
它应该是$n=$targetFile
但首先-清理代码;)
我帮你清理了。检查这是否有效
<?php
session_start();
include('/applications/MAMP/htdocs/connect.php');
mysqli_select_db("people");
if (!isset($_REQUEST['folder'])) $_REQUEST['folder'] = "";
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
move_uploaded_file($tempFile,$targetFile);
$i = "UPDATE users SET filelocation = '$targetFile' WHERE sessionusername = '{$_SESSION['username']}'";
$q = mysqli_query($i);
//debug
echo "$targetFile<br/>
$tempFile<br/>
$i<br/>";
echo mysqli_error();
}
else {
echo "No file";
}
?>
$n=$file['name']
;将不返回任何内容,因为$file未在任何位置声明。您可能希望使用:
$n = $_FILES['Filedata']['name'];
或者任何您真正想要放入数据库的声明;-) 在哪里定义变量$u
?你能发布你的数据库结构吗?$\u FILES['Filedata']['name']
只给他文件的临时名称,他需要$targetFile
不,你在描述$\u FILES['Filedata']['tmp\u name'],但实际上$targetFile包含了完整的操作路径。无论如何,这里不需要声明新变量,$targetFile可以在查询中使用,而且在$n声明之后它会再次被操作。@Dobiatowski:这是不正确的。文件的临时名称存储在$\u文件['Filedata']['tmp\u name']
中。啊,这段代码写得太差,格式太差了,我甚至都没见过print$i
和print mysqli\u error()
在查询后查看succeeds@sarthaksss当它不工作时,你必须学会调试。请应用我的建议:通过打印$i检查SQL查询的真实情况,并通过在查询后打印mysqli_error()检查是否存在SQL错误。您还可以检查$q返回true(查询成功)或false(有错误,请检查错误)嘿$n=$targetFile不工作…数据库重新混音相同。如果我回显这两个值,则不会显示任何内容。文件上载成功,但名称不变$u也匹配数据库。@dobiatowski你能帮我吗??