How do i insert hidden input id from a form to database in php?

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

    How do i insert hidden input id from a form to database in php?

    Hello ,
    I have a form in post.php page . When i submit that , its inserting only the comments not the id of the post .

    This is my form in post.php page .

    Code:
    //Details of post.php
    <form action="comment.php" method="post">
       <input type="hidden" name="pid" value="<? $row['pid'];?>"/>
     <input type="hidden" name="id" value="<? $row['id'];?>"/>
       <input type="text" name="comment" value=""/>
       <input type="submit" name="submit" Value="Submit"/>
    </form>
    
    <?php include('[B]func.php[/B]'); ?>
    
    //End details of post.php
    This is func.php code which is in post.php page . This retrieves the id of the post title and displays the details of the post in post.php page .

    Code:
    <?php
    
    // Connection data (server_address, database, username, password)
    	$dbhost = 'localhost';
    	$dbname = 'project';
    	$dbuser = 'root';
    	$dbpass = '';
    
    
    	$pid=$_GET['pid'];
    	
    	// 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("SELECT * FROM post WHERE pid ='$pid'");
    
    			$query->execute();
            }
    		
    	catch (PDOException $e) 
    		{
    			error_log($e->getMessage());
    			die($e->getMessage());
            }	
    	
    
    	while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
    
    
    echo	"<div id='box' >";
    
    echo	"<h3 class='title'>" .$row['title']. "&nbsp;&ndash;&nbsp;<span class='landmark'>".$row['area']."</span></h3>";
    
    echo	"<p class='address'>".$row['address']."</p>";
    	
    echo	"</div>";	
    
    	}
    $conn = null;
    ?>
    This is the comment.php page to insert form details .

    Code:
    <?php
    
    	// Connection data (server_address, database, username, password)
    	$dbhost = 'localhost';
    	$dbname = 'project';
    	$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 
            {	$lid = $conn->lastInsertId();
    			$query = $conn->prepare("INSERT INTO `comment` (pid,comment,id,date) VALUES (:pid, :comment, :id, :date)");
    
    			$query->execute(
    							array(
    									'pid'     => $_POST['pid'],
    									'comment'   => $_POST['comment'],
    									'id'    	=> $_POST['id'],
    									'date'     => $_POST['date'],
    									)); 
    			$lastId = $conn->lastInsertId();	
    		}
    		
    		
    	catch (PDOException $e) 
    		{
    			error_log($e->getMessage());
    			die($e->getMessage());
            }
    		
       $conn = null;    
    
    	header('location:home.php');
    	
    	exit(); 
    	
    ?>
    Not able to insert post-id ( pid ) and user-id ( id ) into comment's table . What am i missing?
    Do help to solve this ?
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    In your post.php, you are outputting your form, then connecting to the database? That makes no sense. You need to connect to the database to get the data before you can output it. Right now you're outputting nothing and then connecting to the database.

    Comment

    • yateesh
      New Member
      • Dec 2012
      • 36

      #3
      @Rabbit
      func.php is not related to form . In my home.php there are posts . So when i click on the title of post , it executes func.php and displays the post details in post.php .

      Not to make post.php code big , i have made a separate file containing php code for selecting the id of post to display in post.php page and included in post.php as func.php . Thats it .

      Form is used to enter comments for that particularly displayed post . So i asked that i am not getting pid and id inserted in my comment table . All the pid values are 0 .
      Got it ?
      If so how do i correct it ?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Your forgot to echo your variable.

        Comment

        • yateesh
          New Member
          • Dec 2012
          • 36

          #5
          Could u tell me where is that ?

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Code:
            <input type="hidden" name="pid" value="<? echo $row['pid'];?>"/>
            <input type="hidden" name="id" value="<? echo $row['id'];?>"/>

            Comment

            • yateesh
              New Member
              • Dec 2012
              • 36

              #7
              That is not working .

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                Please post your full post.php code along with the html that the client sees.

                Comment

                • yateesh
                  New Member
                  • Dec 2012
                  • 36

                  #9
                  here is post.php

                  Code:
                  <!DOCTYPE html>
                  
                  <head>
                  
                  <link href="css/reset.css" rel="stylesheet"/>
                  <link href="css/post.css" rel="stylesheet"/>
                  
                  
                  </head>
                  
                  <body>
                  	<div id="wrapper">
                  	<div id="disp">
                  	</div>
                  		<header>
                  		
                  			<div id="login">
                  			
                  				<ul>
                  					<li class="logout"><a class="button lightbox" href="logout.php">Log Out</a></li>
                  				</ul>
                  			</div>
                  			
                  			<a style="position:relative;" class="logotext" href="home.php"><span>beez</span>treat</a>
                  			
                  			<input id="search" type="search" placeholder="Who likes to rock the party ?" autocomplete="off">
                  			
                  		</header>
                  	
                  		<nav id="top">
                  			<ul>
                  				<li><a href="#">Home</a></li>/
                  				<li class="current"><a href="home.php">Menu 1</a></li>/
                  				<li><a href="#">Menu 2</a></li>/
                  				<li><a href="#">Menu 3</a></li>/
                  				<li class="last"><a href="#">Menu 4</a></li>
                  			</ul>	
                  		</nav>
                  		
                  		<div id="box">
                  			<h1 class="title"><span class="landmark"></span></h1>
                  			<p class="address"></p>
                  		</div>
                  		
                  		<nav>
                  			<form action="comment.php" method="post">
                  		<input type="hidden" name='pid' value="<? echo $row['pid']); ?>" />
                  				<input type="hidden" name="id" value="<? echo $row['id'];?>"/>
                  				<input type="text" name="comment" value=""/>
                  				<input type="submit" name="submit" Value="Submit"/>
                  			</form>
                  		<nav>
                  
                  		<footer>
                  			<p> Copyright @ 2013 . All Rights Reserved . </p>
                  			<p class="list">Enter your business here:<a href="#formi" class="lightbox">Add Listing</a> </p>
                  		</footer>
                  		
                  		
                  	</div>	
                  	<?php include('postfunc.php'); ?>	
                  </body>
                  
                  </html>
                  The url on post.php page is /post.php?pid=45 for the post with id=45 .

                  This might be simple to you but i am newbie learning all these for my real project . So do help me in solving this . Thank you .

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    That can't possibly be your full post.php. Your $row variable isn't defined anywhere.

                    You have a $row in func.php but like you said earlier, that php has nothing to do with the code above it.

                    Comment

                    • yateesh
                      New Member
                      • Dec 2012
                      • 36

                      #11
                      I have displayed all the codes to you .

                      The below code is to display all the posts from database in home.php .
                      Code:
                      <?php
                      
                      	// Connection data (server_address, database, username, password)
                      	$dbhost = 'localhost';
                      	$dbname = 'project';
                      	$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("SELECT * FROM post ORDER by pid DESC limit 0,5");
                      			$query->execute(); 
                              }
                      		
                      	catch (PDOException $e) 
                      		{
                      			error_log($e->getMessage());
                      			die($e->getMessage());
                              }	
                      	
                      	
                      	while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
                      	echo	"<article id='display'>";
                      	
                      	echo	"<div class='item'>";
                      
                      	echo	"<a href='#' >";
                      
                      	echo	"</a>";
                      
                      	echo	"<div class='content'>";
                      
                      	echo	"<div class='details'></div>";
                      
                      	echo	"<h2><a href='post.php?pid=".$row['pid']."' class='title'>"
                      			.$row['title']."</a></h2>";
                      
                      			
                      
                      	echo	"<h6 class='single'><a href='post.php?pid=".$row['pid']."'>"
                      			.$row['area']."</a></h6>";
                      
                      	$data=$row['address'];
                      
                      	$data1=myTruncate($data, 100, ' ');
                      
                      	
                      	
                      	echo	"<p class='review'>".$data1."</p><a  href='post.php?pid="
                      	.$row['pid']."  '
                      	class='readmore'>Read More &rarr;</a>";
                      	
                      	
                      	
                      	echo	"<div id='comment' style='border:1px solid #cacaca;width:200px;
                      	height:300px;display:none;'>";
                      	echo	"</div>";
                      	
                      			
                      	echo	"</div>";
                      	echo	"</div>";
                      	echo	"</div>";
                      
                      	echo	"<article>";
                      	}
                      	
                      $conn = null;
                      ?>
                      Thats it . No more codes are left .

                      I hope now you could find me the solution .

                      Comment

                      • Rabbit
                        Recognized Expert MVP
                        • Jan 2007
                        • 12517

                        #12
                        The problem has been the same thing I've been saying in post #2 and post #10.

                        Your $row variable isn't defined anywhere. On lines 3 and 4 in your first block of code, you can't use $row because it's nothing. You haven't defined it. You need to populate $row before you can actually get any value out of it.

                        Edit
                        Looking at your home.php. I can see you have $row['pid'] in there. You seem to think that post.php knows the value of $row['pid'] from home.php. That is wrong. Pages don't have access to each others variables. You have to pass them from page to page or store them in session variables.
                        Last edited by Rabbit; Jan 30 '13, 11:44 PM.

                        Comment

                        • yateesh
                          New Member
                          • Dec 2012
                          • 36

                          #13
                          Okay . What you say is right . But how do i pass them as you said from page to page ?

                          Comment

                          • Rabbit
                            Recognized Expert MVP
                            • Jan 2007
                            • 12517

                            #14
                            You're already passing variables in some of your pages. You can use POST or GET. In fact, in your home.php, you're passing pid to post.php through GET. In your post.php, you can retrieve that value from the $_GET array.

                            I have no idea where your user id is coming from, but you'll have to either pass it the same way or retrieve it some other way. You've given no information on the user id so that's all I can say.

                            Comment

                            Working...