Undefined index error after submitting the form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yateesh
    New Member
    • Dec 2012
    • 36

    #1

    Undefined index error after submitting the form

    This is the code to display the details (postfunction.ph p)
    Code:
    <?php
    
    	// Connection data (server_address, database, username, password)
    	$dbhost = 'localhost';
    	$dbname = 'posting';
    	$dbuser = 'root';
    	$dbpass = '';
    
    	// Display message if successfully connect, otherwise retains and outputs the potential error
    	
    	try {
    		$conn = new PDO("mysql:host=$dbhost; dbname=$dbname", $dbuser, $dbpass);
    			     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    	}
    	
    	catch(PDOException $e) 
    		{
    			echo $e->getMessage();
    		}
    	
    	try 
            {
    			$query = $conn->prepare("SELECT * FROM review WHERE pid = :pid");
    			$query->execute(array(':pid' => $_GET['pid'],)); 
            }
    		
    	catch (PDOException $e) 
    		{
    			error_log($e->getMessage());
    			die($e->getMessage());
            }	
    	
    
    	while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
            //Details..
            }
    	$conn=null;
    Theres a form in that page to submit the review .
    When i submit the form it displays an error saying :Undefined index: pid in C:\wamp\www\New \signup\postfun c.php on line 25

    Heres the form
    Code:
    <form action="ins.php" method="post">
       <input type="hidden" name="pid" value="<? . $row['pid'] . ?>" />
       <input type="hidden" name="id" value="<? . $row['id'] . ?>" />
       <input type="text" name="review" />
       <input name="submit" type="submit" value="Submit"/>
    </form>
    Line 25 is : $query->execute(array( ':pid' => $_GET['pid'],));

    I have used $_GET['pid'] because on my other page there are links . When i click on those , they get displayed in this page (postfunction.ph p) by getting the pid of those links .

    What am i missing ?
  • Anas Mosaad
    New Member
    • Jan 2013
    • 185

    #2
    You are trying to retrieve data from a get request while your form is POST not get.

    Comment

    • yateesh
      New Member
      • Dec 2012
      • 36

      #3
      The code for inserting form data is below :
      Code:
      <?php
      
      	// Connection data (server_address, database, username, password)
      	$dbhost = 'localhost';
      	$dbname = 'posting';
      	$dbuser = 'root';
      	$dbpass = '';
      
      	// Display message if successfully connect, otherwise retains and outputs the potential error
      	
      	try 
      		{
      			$conn = new PDO("mysql:host=$dbhost; dbname=$dbname", $dbuser, $dbpass);
      			echo 'Connected to database';
      			$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      		}
      	
      	catch(PDOException $e) 
      		{
      			echo $e->getMessage();
      		}
      
      	try 
              {
      			$query = $conn->prepare("INSERT INTO `comment` (review,pid,id) VALUES (:review, :pid,:id)");
      
      			$query->execute(
      							array(
      									'review'  => $_POST['review'],
      									'pid'     => $_POST['pid'],
      									'id'      => $_POST['id'],
      									)); 
              }
      
      	catch (PDOException $e) 
      		{
      			error_log($e->getMessage());
      			die($e->getMessage());
              }
      	
         $dbh = null;    
      
      	header('location: post.php');
      	
      	exit(); 
      	
      ?>
      I want to insert form data into this table

      Code:
      [B]rid[/B]   -  //id of review table
      [B]review[/B]   -   //review text entered by user
      [B]pid[/B]   -   //id from review table
      [B]id [/B]  -   //id from user table
      So when i submit , it displays that error .
      postfunction.ph p is only for displaying the details of the review . There is no form for that .
      How do i solve this . I am confused in linking these .

      Comment

      Working...