I'm trying to set a variable to a function that selects a table from a database. It has worked all the other times I have been using the function, but now I get this error message:
Notice: Undefined variable: post in C:xampphtdocslogsingle.php on line 50
Now when I try to use another function that I have and set the same variable it doesn't work either. For some reason on this single.php page it will not recognize the function that I have in another file. The file where functions are lies included in posts.php, in a file called db.php, like this:
single.php :
<?php include('path.php');?>
<?php include('posts.php');
if (isset($_GET['id']))
{
$post = selectOne('posts', ['id' => $_GET['id']]);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
<!-- Custom Styles -->
<link rel="stylesheet" href="assets/css/style.css">
<title><?php echo $post['title'];?> </title>
</head>
<body>
<div id="fb-root"></div>
<!-- <script>
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s);
js.id = id;
js.src =
'https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v3.2&appId=285071545181837&autoLogAppEvents=1';
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script> -->
<!-- header -->
<?php include(ROOT_PATH . "/app/includes/header.php"); ?>
<!-- // header -->
<!-- Page wrapper -->
<div class="page-wrapper">
<!-- content -->
<div class="content clearfix">
<div class="page-content single">
<h2 style="text-align: center;"><?php echo $post['title']; ?></h2>
<br>
<?php echo html_entity_decode($post['body']); ?>
</div>
<div class="sidebar single">
<!-- fb page -->
<!-- // fb page -->
<!-- Popular Posts -->
<div class="section popular">
<h2>Popular</h2>
<div class="post clearfix">
<img src="images/image_1.png">
<a href="#" class="title">How to act inspite of your emotions</a>
</div>
<div class="post clearfix">
<img src="images/image_2.png">
<a href="#" class="title">How to act inspite of your emotions</a>
</div>
<div class="post clearfix">
<img src="images/image_3.png">
<a href="#" class="title">How to act inspite of your emotions</a>
</div>
<div class="post clearfix">
<img src="images/image_4.png">
<a href="#" class="title">How to act inspite of your emotions</a>
</div>
<div class="post clearfix">
<img src="images/image_5.png">
<a href="#" class="title">How to act inspite of your emotions</a>
</div>
</div>
<!-- // Popular Posts -->
<!-- topics -->
<div class="section topics">
<h2>Topics</h2>
<ul>
<a href="#">
<li>Poems</li>
</a>
<a href="#">
<li>Quotes</li>
</a>
<a href="#">
<li>Fiction</li>
</a>
<a href="#">
<li>Biography</li>
</a>
<a href="#">
<li>Motivation</li>
</a>
<a href="#">
<li>Inspiration</li>
</a>
<a href="#">
<li>Life Lessons</li>
</a>
<a href="#">
<li>Self Development</li>
</a>
</ul>
</div>
<!-- // topics -->
</div>
</div>
<!-- // content -->
</div>
<!-- // page wrapper -->
<!-- FOOTER -->
<?php include(ROOT_PATH . "/app/includes/footer.php"); ?>
<!-- // FOOTER -->
<!-- JQuery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Slick JS -->
<script type="text/javascript" src="//cdn.jsdelivr.net/npm/[email protected]/slick/slick.min.js"></script>
<script src="assets/js/script.js"></script>
</body>
</html>
posts.php:
<?php
include("app/database/db.php");
include("app/helpers/validatePost.php");
$table = 'posts';
$topics = selectAll('topics');
$posts = selectAll($table);
$errors = array();
$title = "";
$id = "";
$body = "";
$topic_id = "";
$published ="";
if (isset($_GET['id'])){
$post = selectOne($table, ['id' => $_GET['id']]);
$id = $post['id'];
$title = $post['title'];
$body = $post['body'];
$topic_id = $post['topic_id'];
$published = $post['published'];
}
if (isset($_GET['delete_id'])){
$count = delete($table, $_GET['delete_id']);
$_SESSION['message'] = "Post deleted succefully";
$_SESSION['type'] = "success";
header("location: " . BASE_URL . "/admin/posts/index.php");
exit();
}
if(isset($_GET['published']) && isset($_GET['p_id'])){
$published = $_GET['published'];
$p_id = $_GET['p_id'];
$count = update($table, $p_id, ['published' => $published]);
$_SESSION['message'] = "Post published state changed";
$_SESSION['type'] = "success";
header("location: " . BASE_URL . "/admin/posts/index.php");
exit();
}
if (isset($_POST['add-post'])){
$errors = validatePost($_POST);
if(!empty($_FILES['image']['name'])){
$image_name = time() . ' _ ' . $_FILES['image']['name'];
$destination = ROOT_PATH . "/assets/images/" . $image_name;
$result = move_uploaded_file($_FILES['image']['tmp_name'], $destination);
if ($result) {
$_POST ['image'] = $image_name;
} else{
array_push($errors, 'failed to upload image');
}
} else{
array_push($errors, "Post image required");
}
if(count($errors) == 0) {
unset($_POST['add-post']);
$_POST['user_id'] = $_SESSION['id'];
$_POST['published'] = isset($_POST['published']) ? 1 : 0;
$_POST['body'] = htmlentities($_POST['body']);
$post_id = create($table, $_POST);
$_SESSION['message'] = "Post created succefully";
$_SESSION['type'] = "success";
header("location: " . BASE_URL . "/admin/posts/index.php");
exit();
} else {
$title = $_POST['title'];
$body = $_POST['body'];
$topic_id = $_POST['topic_id'];
$published = isset($_POST['published']) ? 1 : 0;
}
}
if(isset($_POST['update-post'])){
$errors = validatePost($_POST);
if(!empty($_FILES['image']['name'])){
$image_name = time() . ' _ ' . $_FILES['image']['name'];
$destination = ROOT_PATH . "/assets/images/" . $image_name;
$result = move_uploaded_file($_FILES['image']['tmp_name'], $destination);
if ($result) {
$_POST ['image'] = $image_name;
} else{
array_push($errors, 'failed to upload image');
}
} else{
array_push($errors, "Post image required");
}
if(count($errors) == 0) {
$id = $_POST['id'];
unset($_POST['update-post'], $_POST['id']);
$_POST['user_id'] = $_SESSION['id'];
$_POST['published'] = isset($_POST['published']) ? 1 : 0;
$_POST['body'] = htmlentities($_POST['body']);
$post_id = update($table, $id, $_POST);
$_SESSION['message'] = "Post updated succefully";
$_SESSION['type'] = "success";
header("location: " . BASE_URL . "/admin/posts/index.php");
} else {
$title = $_POST['title'];
$body = $_POST['body'];
$topic_id = $_POST['topic_id'];
$published = isset($_POST['published']) ? 1 : 0;
}
}
db.php: (There are more functions, but i only included the two I tried to use with the variable $post.
function selectOne($table, $conditions)
{
global $conn;
$sql = "SELECT * FROM $table";
//return srecords that match conditions
$i = 0;
foreach($conditions as $key => $value) {
if ($i === 0){
$sql = $sql . " WHERE $key=?";
} else{
$sql = $sql . " AND $key=?";
}
$i++;
}
$sql = $sql . " LIMIT 1";
$stmt = executeQuery($sql, $conditions);
$records = $stmt->get_result()->fetch_assoc();
return $records;
}
function dd($value){
echo "<pre>", print_r($value, true), "</pre>";
die();
}