Undefined Index error for checkbox

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!


  $title = mysqli_real_escape_string($db, $_POST['title']);
  $body = mysqli_real_escape_string($db, $_POST['body']);
  $published = mysqli_real_escape_string($db, $_POST['published']);
    $id = mysqli_real_escape_string($db, $_POST['id']);
    $query = "UPDATE pages SET title='$title',body='$body',published='$published' WHERE id='$id'";
    $query = "INSERT INTO pages (title,body,published)
    VALUES('$title', '$body', '$published')";

  $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">
		  Page Title : 
		  <input class="form-control" value="<?php echo @$p['title']; ?>" name="title" >
	      <div class="form-group">
		  Post Body : 
		  <textarea name="body" class="form-control" ><?php echo @$p['body']; ?></textarea>
	      <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?
	      <button type="submit" name="Update_Page" class="btn btn-primary pull-right">Update Page</button>

…Within a condition.
The variable is only set if the form has been submitted.

1 Like

It’s fixed! I passed the isset() function at the beginning of the code where I’m gathering the data from the form below. Thank You!

What some do to prevent this is to set variables with blank strings at the start of the script so they are defined even if there is no content.

$published = '' ;
    $published = preg_replace('#[^0-1]#i', '', $_POST['published']);
    // Other stuff

Checkbox fields are only sent to PHP if they’ve been checked. If they’ve not been checked, they don’t get sent, hence the undefined index error as there was nothing sent:

if (isset($_POST['published'])) {
	$published = 1;
} else {
	$published = 0;

That example would handle the published field

btw, have a read up on the use of prepared statements:


They are far superior to any “real_escape_string” functions


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.