为什么这个php代码不会将其数据发送到数据库?

时间:2013-06-20 19:25:56

标签: php mysql

我之前问了一个像这样的问题,但我不得不改变我的代码,我仍然不明白为什么我的数据库即使在我提交条目后也是空的。我是php的新手,我正在阅读一本书,您可以在http://www.apress.com/9781430224730找到源代码。

运行代码之后我没有收到错误消息,但是我的代码没有按照我想要的方式运行,我相信这是因为没有数据被放到数据库中,这是其余代码所依赖的上。

我应该将数据发送到数据库的代码从第66行开始,我已经为你及其旁边的else语句注释了它。

无论出于何种原因我的代码只是向数据库发布任何内容我非常感谢任何帮助

这是我的代码:

<?php

// Include the functions so we can create a URL
include_once 'functions.inc.php';
include_once 'images.inc.php';

//$e = array();

if($_SERVER['REQUEST_METHOD']=='POST'
    && $_POST['submit']=='Save Entry'
    && !empty($_POST['page'])
    && !empty($_POST['title'])
    && !empty($_POST['entry']))
{
    // Create a URL to save in the database
    $url = makeUrl($_POST['title']);
    echo "here";
    if(isset($_FILES['image']['tmp_name']))
    {
        try 
        {
            $img = new ImageHandler("/simple_blog/images/");//not in the textbook
        //print_r($_FILES);
        //exit;
        $img_path = $img->processUploadedImage($_FILES['image']);
        echo '<img src="', $img_path,'" /><br />';//This prints out the image
        }
        catch(Exception $e)
        {
        die($e->getMessage());
        }
    }
    else
    {
        $img_path = NULL;
    }

    // Include database credentials and connect to the database
    include_once 'db.inc.php';
    $db = new PDO(DB_INFO, DB_USER, DB_PASS);

    echo "Image Path: ", $img_path, "<br />";
    //exit; in the book but not on the source
    echo "here";
    // Edit an existing entry
    if(!empty($_POST['id']))
    {
        $sql = "UPDATE entries
            SET title=?, entry=?, url=?
            WHERE id=?
            LIMIT 1";
        $stmt = $db->prepare($sql);
        $stmt->execute(
            array(
            $_POST['title'],
            $img_path,
            $_POST['entry'],
            $url,
            $_POST['id']
            )
        );
        $stmt->closeCursor();
    }

// Create a new entry
    else               //line 66
    {
        //Save the entry into the database
        $sql = "INSERT INTO entries (page, title, image, entry, url)
            VALUES (?, ?, ?, ?, ?)";
        $stmt = $db->prepare($sql);
        $stmt->execute(array($_POST['page'], $_POST['title'], $img_path, 
        $_POST['entry'], $url));
        $stmt->closeCursor();

    // Sanitize the page information for use in the success URL
    $page = htmlentities(strip_tags($_POST['page']));

    // Send the user to the new entry
    echo "hopefully we get here";
    header('Location: /simple_blog/'.$page.'/'.$url);
    exit;
    }
}
else
{
header('Location: ../');
exit;
}

?>

1 个答案:

答案 0 :(得分:-1)

您必须使用$ stmt-&gt; bind_result将结果绑定到查询。之后你必须在没有任何参数的情况下执行它。