Undefined index id in PHP

Hello,
I am new in PHP and I am trying to build a simple app using PHP and MySQL.
With CRUD.
I can insert the post on the database with edit option, but when I am trying to update a post, it is showing “undefined index id in PHP”.

Here is my full code. https://github.com/ehsanatwork/railsblog
can any one please help me.

Thank You

Hi @ehsanatrajit, it looks like you’re not sending the post ID with your update form, which is why there is no $_POST['id']… so you might just add a hidden ID field there.

Thank You for your reply,
I have added the hidden input field in edit.php file line 20.

<input type="hidden" name="id" value="<?php echo $id; ?>">

Since DEBUGGING is far more difficult than programming I prefer to write verbose scripts which are easier to read, amend, update, etc,

Try this:

<?php
declare(strict_types=1);

	error_reporting(-1);
	ini_set('display_errors', 'TRUE');

	require 'dbh.php';

	$id = isset($_POST['id']) 
		? $_POST['id'] 
		: NULL;
	$title = isset($_POST['title'])
 		 ? $_POST['id']
		 : NULL;
	$description = isset($_POST['description'])
				 ? $_POST['id'] 
				 : NULL;
	$pubdate = isset($_POST['pubdate'] )
			 ? $_POST['id']
			 : NULL;
	$author = isset($_POST['author']) 
			? $_POST['id'] 
			: NULL;

	if( isset( $id, $title, $description, $pubdate, $author ) ):
		$sql = <<< ____TMP
			UPDATE
				posts 
			SET 
				title="$title", 
				description="$description", 
				pubdate="$pubdate", 
				author="$author" 
			WHERE
				id="$id";
____TMP;
        // REMOVE //  TO display $sql
		// echo '<pre>'.$sql .'</pre>'; die;	
		$result = $conn->query($sql);
		if ($result === TRUE):
			echo "Record updated successfully";
		else:
			echo "Error updating record: " . $conn->error;
		endif;

	else:
		echo '$id is NOT SET ???';
	endif;

thank you.
but again it is showing id is not set error.

Try displaying the following parameters:

// TEST $_GET and $_POST variables
if( 1 ): // change 1 to 0 to prevent showing
  echo '<pre>';
     echo '$_GET => ';
     print_r( $_GET );.
     echo '<hr>';

     echo '$_POST => ';
     print_r( $_POST );.
     echo '<hr>';
  echo '</pre>';
  die;
endif;
// 

id is not set.

Try inserting the script from Post 6

I always have this function for DEBUGGING:

<?php 
/* 
   usage:
     fred( 'ANY VARIABLE', 'TITLE GOES HERE' );
    OR
      fred($_GET);
    OR
      fred($_POST);
*/
function fred( $val="NOTHING SET", $title = 'Yes we have no $title???')
{
  echo '<pre>';
    echo $title;
    print_r( $val );
  echo '</pre>';
}//endfunc

The problem in the version of edit.php in that link is:

<input type="hidden" name="id" value="<?php echo $id; ?>"> 
	<article id="articles"> 
	<h2 class="text-center">Edit This post</h2> 
	<form action="update.php" method="POST" role="form"> 

You’ve added the hidden input field, but you’ve added it before the <form> open tag, so it doesn’t get sent. Move it so it’s between the form open and close tags and it should work.

It’s dangerous to use all these POST and GET variables without checking that they exist, and that they’re in a decent form. Presumably you’re going to add error-checking and security in later on.

1 Like

Another problem might be that you’re setting

$id = $_GET['id'];

in your edit.php before fetching and iterating over the results. It should probably be

$id = $result['id'];

within the foreach loop, so that update.php knows which post to actually update.

As I read it, edit.php is called from the index.php which lists all the posts, each one with a link to edit that post using edit.php. The post id is passed into edit as part of the URL, hence it’s used as $_GET['id']. That code then shows the post in an edit form, and calls update.php to store it with a form post. This last one is where the hidden variable is in the wrong place.

Ah yes you’re right. I thought edit.php would again list all posts as separate forms, but I haven’t looked at the where clause closely enough (and the loop, for that matter)… so that ID is indeed right.

yes, you are correct,
i should bring that input field inside form.
and now it works fine.
Thanks to all for your help.

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