如何使用PDO在数据库中插入数据?

时间:2019-05-09 20:10:20

标签: php sql

我的代码中是否有任何错误?我试图插入文章,但是没有插入。

如果有人可以检查我的代码并告诉我我的错误(我正在学习),那就太好了。

<?php 
session_start();
include_once('../includes/connection.php');

if (isset($_SESSION['logged_in']))
{
    //display add page
    if (isset($_POST['title'], $_POST['content']))
    {
        $title = $_POST['title'];
        $content = $_POST['content'];

        if (empty($title) or empty($content))
        {
            $error = 'All fields are required';
        }
        else
        {
            $query = $pdo->prepare('INSERT INTO articles (article_title, article_content, article_timestamp) VALUES (?, ?, ?)');

            $query->bindValue(1, $title);
            $query->bindValue(2, $content);
            $query->bindValue(3, time());

            $query->execute();

            header('Location: index.php');
            exit();
        }
    }
?>
<html>
<head>
    <title>CMS Tuterial</title>
    <link rel="stylesheet" href="../assets/style.css" />
</head>
<body>
    <div class="container">
        <a href="index.php" id="logo">CMS</a>
        <br />
        <h4>Add Article</h4>
<?php
    if(isset($error))
    {
?>
        <small style="color:#aa0000;"><?php echo $error; ?>
        <br /> <br />

<?php
    }
?>
        </small>
        <form action="add.php" method="post" autocomplete="off">
            <input type="text" name="title" placeholder="Title" /><br /><br />
            <textarea rows="15" cols="50" placeholder="Content" name="content"></textarea><br /><br />
            <input type="submit" value="Add Article" />
        </form>
    </div>
</body>
</html>
<?php
}
else
{
    header('Location: index.php');
}
?>

1 个答案:

答案 0 :(得分:0)

This doc解释了您可能要检查的PDOStatement::bindValue。然后,您可能需要将代码更改为与此类似的内容,并且可能会起作用:

session_start();

include_once '../includes/connection.php';

if (isset($_SESSION['logged_in'])) {
    //display add page
    if (isset($_POST['title'], $_POST['content'])) {
        $title = $_POST['title'];
        $content = $_POST['content'];

        if (empty($title) or empty($content)) {
            $error = 'All fields are required';
        } else {
            $query = $pdo->prepare('INSERT INTO articles (article_title, article_content, article_timestamp) VALUES (?, ?, ?)');

            $query->bindValue(1, $title, PDO::PARAM_STR);
            $query->bindValue(2, $content, PDO::PARAM_STR);
            $query->bindValue(3, time(), PDO::PARAM_INT);

            $query->execute();

            header('Location: index.php');
            exit();
        }
    }
}

编辑:

如果只希望看到查询正在执行,则可以删除if并进行测试,然后可以添加任何必要的if。也许类似于:

session_start();

include_once '../includes/connection.php';

$title = $_POST['title'];
$content = $_POST['content'];
var_dump($title);
var_dump($content);
$query = $pdo->prepare('INSERT INTO articles (article_title, article_content, article_timestamp) VALUES (?, ?, ?)');
var_dump($query);
$query->bindValue(1, $title, PDO::PARAM_STR);
$query->bindValue(2, $content, PDO::PARAM_STR);
$query->bindValue(3, time(), PDO::PARAM_INT);
$query->execute();