-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 に答える