I am working on creating a very simple content management system... Unfortunately I'm failing to retrieve posts from my database. My error is:
Notice: Undefined index: title in C:\\wamp\\www\\NightOwlSoftware\\index.php
<?php
include 'scripts/db_connect.php';
include 'scripts/functions.php';
sec_session_start();
$sql = "SELECT * FROM blog";
$result = mysqli_query($mysqli, $sql);
while($row = mysqli_fetch_array($result)) {
echo'<div class="blog"><h3 class="blog">' . $row['title'] . "</h3>";
echo'<span class="blog"> Date: ' . $row['date'] . " Tag: " . $row['tag'] . "</span><hr>";
echo'<p class="blog">' . $row['body'] . "</p>";
}
?>
Here is the working script that stores data proving that my columns are all there...
<?php
include 'db_connect.php';
include 'functions.php';
sec_session_start();
$title = $_POST['title'];
$body = $_POST['body'];
$tag = $_POST['tag'];
$date = date_create()->format('Y-m-d H:i:s');
$sql = "INSERT INTO blog (date, title, body, tag)
VALUES ('$date', '$title', '$body', '$tag')";
mysqli_query($mysqli, $sql);
mysqli_close($mysqli);
header( 'Location: ../index.php' ) ;
?>
If that's the only error you're getting (ie. date, tag, body all work fine), then you probably mis-typed something in the database creation, and don't actually have a title
column as a result. Or maybe the column has a different name, like name
, subject
or im_so_bored_i_dont_know_what_im_typing
... (sorry, I'm bored!)
You are getting an undefined index for title
because there is no title
in the array $row
.
I would recommend you look over your DB structure for the table blog
but I would also recommend doing some basic data checks in your while
to ensure you render only the content that exists. Look this over:
<?php
include 'scripts/db_connect.php';
include 'scripts/functions.php';
sec_session_start();
$sql = "SELECT * FROM blog";
$result = mysqli_query($mysqli, $sql);
while($row = mysqli_fetch_array($result)) {
if (array_key_exists('title', $row) && !empty($row['title'])) {
echo'<div class="blog"><h3 class="blog">' . $row['title'] . "</h3>";
}
if (array_key_exists('date', $row) && !empty($row['date'])) {
echo '<span class="blog"> Date: ' . $row['date'];
}
if (array_key_exists('tag', $row) && !empty($row['tag'])) {
echo " Tag: " . $row['tag'] . "</span><hr>";
}
if (array_key_exists('body', $row) && !empty($row['body'])) {
echo'<p class="blog">' . $row['body'] . "</p>";
}
}
?>
If this all runs & nothing renders, then your DB query is flawed, so data is not returned. A simple way to check this before the while
loop is to dump the array to the screen to see what is there:
<?php
include 'scripts/db_connect.php';
include 'scripts/functions.php';
sec_session_start();
$sql = "SELECT * FROM blog";
$result = mysqli_query($mysqli, $sql);
while($row = mysqli_fetch_array($result)) {
echo '<pre>';
print_r($row);
echo '</pre>';
[ rest of your code goes here]
If I could, I would've added this as a comment because this is not really an answer. I agree with other, that most likely - the column name in DB does not match what you have in the code.
In any case, when I am in situations like this, you could use debugger or use print_r.
In your while loop, add the print_r statement -
while($row = mysqli_fetch_array($result)) {
print_r($row);
echo'<div class="blog"><h3 class="blog">' . $row['title'] . "</h3>";
echo'<span class="blog"> Date: ' . $row['date'] . " Tag: " . $row['tag'] . "</span><hr>";
echo'<p class="blog">' . $row['body'] . "</p>";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.