1

我有一个php脚本如下:

function addPost(BlogPost $item, $tags) {
        $connection = mysql_connect('localhost', '***', '***') or die(mysql_error());
        mysql_select_db('jschaible1') or die(mysql_error());

        $queryString = "insert into BlogPost values ( null, '" . $item->Title . "', '" . $item->Body . "', " . "now());";

        $result = mysql_query($queryString) or die(mysql_error());

        $dbResult = mysql_query('select * from blogpost where Title = "' . $item->Title . '";') or die(mysql_error());
        while ($row = mysql_fetch_array($dbResult)) {
            $tableID = $row['BlogPostID'];
        }

        foreach($tags as $t) {
            $queryString = "insert ignore into Tag values('" . strtolower($t) . "');";
            mysql_query($queryString) or die(mysql_error());

            $queryString = "insert into blogposttag values (" . $tableID . ", '" . strtolower($t) . "');";
            mysql_query($queryString) or die(mysql_error());
        }

        echo $connection;
        mysql_close($connection) or die(mysql_error());
    }

该函数的调用方式如下:

<?php
    session_start();

    $errors = '';

    if (!isset($_SESSION['dadfg6d5f6df54']))
        header('Location:admin.php');
    else {
        include('Classes.php');
        include('mySql.php');
        include('utils.php');

        if(isset($_POST['Submit'])) {
            if ($_POST['Title'] == '') {
                $errors = 'Post must have a title!';
            }
            else if ($_POST['PostBody'] == '') {
                $errors = 'Post must be something!';
            }
            else if (strlen($_POST['PostBody']) < 10) {
                $errors = "Write something substantial, c'mon!";
            }
            else if ($_POST['Tags'] == '') {
                $errors = "At least one tag must be entered";
            }
            else {
                $newPost = new BlogPost(NULL, sanitize($_POST['Title']), sanitize($_POST['PostBody']), NULL);

                $newPost->Title = addEmoticons($newPost->Title);
                $newPost->Body = addEmoticons($newPost->Body);

                $tags = str_replace(',', '', $_POST['Tags']);
                $tags = str_replace(';', '', $tags);
                $tags = explode(' ', $tags);

                error_reporting(E_ALL); ini_set('display_errors', 1);
                addPost($newPost, $tags) or die();

                $errors = 'Post added successfully';
            }
        }
    }
?>

当它到达 mysql_close() 时,页面停止执行,我得到一个空白页面。这真的让我很沮丧,我完全不明白为什么会这样,尤其是看到 PREVIOUS 行上的回声如何输出“资源 id#6”。我没有收到错误消息,只是一个空白页!请帮忙!

4

1 回答 1

2

您的函数没有返回值。因此,它返回NULL评估为“假”值。由于您通过or die()调用跟踪它,因此错误评估会触发or die()并终止您的脚本。

// Don't do this:
addPost($newPost, $tags) or die();

// Do this:
addPost($newPost, $tags);

在你的函数结束时,你可以return TRUE,但除非你希望根据你添加的成功或失败返回一个值,否则完全没有必要。你拥有它的方式,die()只会造成不应有的伤害。由于函数中所有潜在的失败点都将在出错时终止脚本,因此返回TRUE. 只需die()在函数调用之后删除。

于 2012-10-31T00:07:05.503 回答