-2

Is there any error in my code? I tried to insert articles but it's not being inserted.

It will be great if someone can check my code and tell me about my mistakes (I'm learning).

<?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');
}
?>
Prix
  • 19,417
  • 15
  • 73
  • 132
adel adeli
  • 11
  • 3

1 Answers1

0

This doc explains the PDOStatement::bindValue that you might want to check. Then, you might want to change your codes to something similar to this, and it may work:

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();
        }
    }
}

Edit:

If you only wish to see, queries are being executed, you might remove the ifs and test it, then you can add any if that is necessary. Maybe, similar to:

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();
Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    `time()` returns an integer. Why list the data type explicitly if `PDO::PARAM_STR` is the default value? – Dharman May 09 '19 at 20:28
  • 1
    Always `exit()` after `header('Location: ...');`, an easy shortcut is `exit(header('Location: ...'));` – Dharman May 09 '19 at 20:29
  • not sloved yet! – adel adeli May 09 '19 at 21:04
  • 2
    @adeladeli What is not solved yet? Can you please tell us what problems you are facing? What are your expectations, too? – Dharman May 09 '19 at 21:07
  • first i'm new at php, i'm practicing to insert articles into the database to fetch it later, i'm writing this code so the articles not bieng inserted and i can't know where is the mistake in my code bcuz i'm not seeing any error in the browser !! – adel adeli May 09 '19 at 21:18
  • and i edited my qustion above so u can see my full code in the url – adel adeli May 09 '19 at 21:22