无法弄清楚为什么PHP函数没有被执行

时间:2013-04-26 09:16:30

标签: php function

在上载文件并将某些表单数据插入数据库的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 Form插件脚本

// 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对象...

3 个答案:

答案 0 :(得分:1)

$ fileName $ title $ speaker $ date is null

你需要传递值,如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

})

答案 1 :(得分:0)

从你的描述来看,这听起来像是错误的:

$inserted = AddSermonToDb();

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

答案 2 :(得分:0)

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

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

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

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