Hey everyone,
So I have a simple form with some simple sql executions to output data from a database. For some reason, I am getting a Undefined index error.
Notice: Undefined index: published in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\my portable files\blogCMS\admin\new_page.php on line 61
I’ve declared that the variable is(set) near the top of the page, yet it still gives me an error for the checkbox I have to check whether or not a page is set to published or not (i.e. 1 or 0). Here is my code for all of it. Any help would be greatly appreciated to figure out where my fault lies. Thank you!
<?php
include('includes/config.php');
include('includes/db.php');
include('includes/header.php');
include('includes/sidebar.php');
if(isset($_POST['Update_Page'])){
$title = mysqli_real_escape_string($db, $_POST['title']);
$body = mysqli_real_escape_string($db, $_POST['body']);
$published = mysqli_real_escape_string($db, $_POST['published']);
if(isset($_POST['id'])){
$id = mysqli_real_escape_string($db, $_POST['id']);
$query = "UPDATE pages SET title='$title',body='$body',published='$published' WHERE id='$id'";
}else{
$query = "INSERT INTO pages (title,body,published)
VALUES('$title', '$body', '$published')";
}
$db->query($query);
}
if(isset($_GET['id'])){
$id = mysqli_real_escape_string($db, $_GET['id']);
$p = $db->query("SELECT * FROM pages WHERE id = '$id'");
$p = $p->fetch_assoc();
}
?>
<h1 class="page-header">Add New Page</h1>
<div class="table-responsive">
<form method="post">
<?php if(isset($p)){
echo "<input type='hidden' value='$id' name='id' />";
} ?>
<div class="form-group">
<label>
Page Title :
</label>
<input class="form-control" value="<?php echo @$p['title']; ?>" name="title" >
</div>
<div class="form-group">
<label>
Post Body :
</label>
<textarea name="body" class="form-control" ><?php echo @$p['body']; ?></textarea>
</div>
<div class="form-group">
<label for="published">
<input type="checkbox" name="published" id="published" class="form-control" value=1
<?php if($_POST['published'] == 1){echo "checked";} ?>
> Publish?
</label>
</div>
<button type="submit" name="Update_Page" class="btn btn-primary pull-right">Update Page</button>
</form>
</div>
</div>
</div>
</div>
</body>
</html>