保存数据库的条目

时间:2013-05-22 22:21:52

标签: php html css

我已经设法与数据库建立连接,以获得一个存储一些条目的简单表单。它确实显示已使用表单提交了条目,但它不显示已提交的信息。相反,它在我在表中创建的Entry列中显示NULL。我需要它来显示提交的内容。这是我的代码:

if($_SERVER['REQUEST_METHOD'] == 'POST'
    && $_POST['submit']=='Save Entry'
    && !empty($_POST['title'])
    && !empty($_POST['entry'])) 
{
    // Include database credentials and connect to the database
    include_once 'db.inc.php';
    $db = new PDO(DB_INFO, DB_USER, DB_PASS);

    // Save the entry into the database
    $sql = "INSERT INTO entries (title, entry) VALUES (?, ?)";
    $stmt = $db->prepare($sql);
    $stmt->execute(array($title, $entry));
    $stmt->closeCusor();

    // Get the ID of the entry we just saved
    $id_obj = $db->query("SELECT LAST_INSERT_ID");
    $id = $id_obj->fetch();
    $id_obj->closeCursor();

    // Continue processing information
}

// If both conditions aren't met, sends the user back to the main page
else {
    header('Location: ../amin.php');
    exit;
}

2 个答案:

答案 0 :(得分:1)

您尚未在任何地方设置$title$entry的值。如果您尝试拨打电话closeCursor(),也会有拼写错误。

答案 1 :(得分:1)

可能register_globals已关闭(因为有充分理由,自PHP 5.3起已弃用)。这意味着您必须从$ _POST数组中读取$title$entry

$title = $_POST['title'];
$entry = $_POST['entry'];