0

在上传文件并将一些表单数据插入数据库的 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 对象......

4

3 回答 3

1

$fileName $title $speaker $date 为空

你需要像 AddSermonToDb($fileName, $title,$speaker,$date) 这样的传递值

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')";
.....

//if you use jquery add 
$(function(){     
 //jquery code

})
于 2013-04-26T09:24:45.990 回答
0

从您的描述来看,这里似乎出错了:

$inserted = AddSermonToDb();

可以肯定的是,只需编写一个简单的 try/catch 即可以可预测的方式处理错误。

于 2013-04-26T09:20:59.350 回答
0

SQL 查询中的变量 $fileName、$title、$speaker 和 $date

INSERT INTO sermons (sermonMp3FileName, sermonTitle, sermonSpeaker, sermonDate)
        VALUES ('$fileName', '$title', '$speaker', '$date')

在函数范围之外定义。由于它们不存在于函数中,因此如果打开警告,它们将出错,如果它们被关闭,它们将返回 null。

这可能会导致您的查询产生意外结果,并可能导致致命错误,这将停止您的代码执行并阻止任何进一步的日志

于 2013-04-26T09:24:47.987 回答