-1

我之前问过一个类似这样的问题,但我不得不稍微更改我的代码,即使我提交了一个条目,我仍然不明白为什么我的数据库是空的。我是 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;
}

?>
4

1 回答 1

-1

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

于 2013-06-20T19:29:10.027 回答