在上传文件并将一些表单数据插入数据库的 PHP 脚本中,不会调用执行数据库插入的函数。看看下面的代码。如您所见,我在整个代码中都有日志语句来跟踪正在发生的事情。您会看到该行$log->lwrite('Successfully moved the file to the destination folder.');
被写入日志文件,但之后没有其他内容被写入日志文件,包括$log->lwrite('$inserted: ' . $inserted);
.
在调用它的 jQuery Form 插件中,上传进度正确显示并达到 100% 并且文件正在上传,但回调的完整部分中的任何代码都没有执行。
请看一下。我看不到我在这里缺少什么。
下面更新
这是我的 PHP 脚本:
require ('logging.php');
$log = new Logging();
$fileName = basename($_FILES['uploadedFile']['name']);
$targetPath = '../media/' . $fileName;
$title = $_POST['sermonTitle'];
$speaker = $_POST['speakerName'];
$date = $_POST['sermonDate'];
$log->lwrite('file name: ' . $fileName . ', title: ' . $title . ', speaker: ' . $speaker . ', date: ' . $date);
if ($_FILES['uploadedFile']['type'] === 'audio/mp3') {
$log->lwrite('Correct file type.');
if (file_exists($targetPath)) {
$log->lwrite('File already exists.');
// File with the selected name already exists on the server
exit ('exists');
} else {
if (move_uploaded_file($_FILES['uploadedFile']['tmp_name'],$targetPath)) {
$log->lwrite('Successfully moved the file to the destination folder.');
$inserted = AddSermonToDb();
$log->lwrite('$inserted: ' . $inserted);
if ($inserted == true) {
$log->lwrite('Added the sermon to the DB.');
exit ('success');
} else {
$log->lwrite('Failed to add the sermon to the DB.');
exit ('insertFail');
}
} else {
$log->lwrite('Couldn\'t upload the file.');
// Problem uploading the file or moving it to the destination folder
exit ('uploadFail');
}
}
} else {
$log->lwrite('Invalid file type.');
exit ('invalidType');
}
function AddSermonToDb() {
$log->lwrite('In AddSermonToDb function');
// Connect to the database
//require_once([path to connection script]);
$query = "INSERT INTO sermons (sermonMp3FileName, sermonTitle, sermonSpeaker, sermonDate)
VALUES ('$fileName', '$title', '$speaker', '$date')";
$log->lwrite('$query: ' . $query);
$result = @mysqli_query($dbc, $query);
$log->lwrite('$result: ' . $result);
mysqli_close($dbc);
if ($result) {
return true;
} else {
return false;
}
}
这是 jQuery 表单插件脚本
// Reset validation and progress elements
var percentVal = '0%';
$('.statusBar').width(percentVal);
$('.percent').html(percentVal);
$('#frmSermonUpload').ajaxForm({
beforeSubmit: function() {
var formValid;
if (!ValidateUploadForm()) {
formValid = false;
} else {
formValid = true;
}
if (!formValid) {
return false;
}
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
$('.statusBar').width(percentVal)
$('.percent').html(percentVal);
},
complete: function(xhr) {
if (xhr.responseText === 'success') {
$('.statusBar').width('100%');
$('.percent').html('100%');
$('#status').html('Successfully uploaded the sermon.<br>Successfully added the sermon to the database.').addClass('successUpload');
ClearForm();
} else if (xhr.responseText === 'uploadFail') {
$('#status').html('There was a problem uploading the file. Try again.<br>If the problem persists, contact the system administrator.').addClass('errorUpload');
} else if (xhr.responseText === 'exists') {
$('#status').html('A file with that name already exists on the server.').addClass('errorUpload');
} else if (xhr.responseText === 'insertFail') {
$('#status').html('The file was uploaded, but there was a problem inserting the information into the database.').addClass('errorUpload');
} else if (xhr.responseText === 'invalidType') {
$('#status').html('Invalid file type. Only audio files with the extention "mp3" are allowed.').addClass('errorUpload');
}
}
}); // End Upload Status Bar
感谢任何人的帮助。
更新
我很尴尬地承认,你们中的一些人指出我没有将变量传递给函数是正确的。
此外,不,我没有检查 PHP 错误日志(我不敢相信我承认所有这些本应如此明显的东西!)。
因此,我将变量添加到函数调用中并检查了 PHP 错误日志,在那里我找到了行Call to a member function lwrite() on a non-object in /home3/fbcglenw/public_html/scripts/sermonUpload.php on line 44
,这是函数中调用 $log 的第一行。
我认为该函数将能够“看到” $log 对象,因此我尝试将 $log 添加到函数调用和函数定义中的变量中,但这并没有改变错误。
更新 #2 我不确定(这就是我在这里寻求帮助的原因),但似乎这是一个范围问题,但如果 $log 对象位于 php 脚本的开头,我会认为该脚本中的任何函数都应该能够“看到”那个 $log 对象......