473,379 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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

36
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 .

Expand|Select|Wrap|Line Numbers
  1. //Details of post.php
  2. <form action="comment.php" method="post">
  3.    <input type="hidden" name="pid" value="<? $row['pid'];?>"/>
  4.  <input type="hidden" name="id" value="<? $row['id'];?>"/>
  5.    <input type="text" name="comment" value=""/>
  6.    <input type="submit" name="submit" Value="Submit"/>
  7. </form>
  8.  
  9. <?php include('func.php'); ?>
  10.  
  11. //End details of post.php    
  12.  
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 .

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // Connection data (server_address, database, username, password)
  4.     $dbhost = 'localhost';
  5.     $dbname = 'project';
  6.     $dbuser = 'root';
  7.     $dbpass = '';
  8.  
  9.  
  10.     $pid=$_GET['pid'];
  11.  
  12.     // Display message if successfully connect, otherwise retains and outputs the potential error
  13.     try 
  14.         {
  15.             $conn = new PDO("mysql:host=$dbhost; dbname=$dbname", $dbuser, $dbpass);
  16.             //echo 'Connected to database';
  17.             $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  18.         }
  19.  
  20.     catch(PDOException $e) 
  21.         {
  22.             echo $e->getMessage();
  23.         }
  24.     try 
  25.         {
  26.             $query = $conn->prepare("SELECT * FROM post WHERE pid ='$pid'");
  27.  
  28.             $query->execute();
  29.         }
  30.  
  31.     catch (PDOException $e) 
  32.         {
  33.             error_log($e->getMessage());
  34.             die($e->getMessage());
  35.         }    
  36.  
  37.  
  38.     while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
  39.  
  40.  
  41. echo    "<div id='box' >";
  42.  
  43. echo    "<h3 class='title'>" .$row['title']. "&nbsp;&ndash;&nbsp;<span class='landmark'>".$row['area']."</span></h3>";
  44.  
  45. echo    "<p class='address'>".$row['address']."</p>";
  46.  
  47. echo    "</div>";    
  48.  
  49.     }
  50. $conn = null;
  51. ?>
  52.  
This is the comment.php page to insert form details .

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     // Connection data (server_address, database, username, password)
  4.     $dbhost = 'localhost';
  5.     $dbname = 'project';
  6.     $dbuser = 'root';
  7.     $dbpass = '';
  8.  
  9.  
  10.     // Display message if successfully connect, otherwise retains and outputs the potential error
  11.  
  12.     try {
  13.             $conn = new PDO("mysql:host=$dbhost; dbname=$dbname", $dbuser, $dbpass);
  14.             //echo 'Connected to database';
  15.             $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  16.     }
  17.  
  18.     catch(PDOException $e) 
  19.         {
  20.             echo $e->getMessage();
  21.         }
  22.  
  23.     try 
  24.         {    $lid = $conn->lastInsertId();
  25.             $query = $conn->prepare("INSERT INTO `comment` (pid,comment,id,date) VALUES (:pid, :comment, :id, :date)");
  26.  
  27.             $query->execute(
  28.                             array(
  29.                                     'pid'     => $_POST['pid'],
  30.                                     'comment'   => $_POST['comment'],
  31.                                     'id'        => $_POST['id'],
  32.                                     'date'     => $_POST['date'],
  33.                                     )); 
  34.             $lastId = $conn->lastInsertId();    
  35.         }
  36.  
  37.  
  38.     catch (PDOException $e) 
  39.         {
  40.             error_log($e->getMessage());
  41.             die($e->getMessage());
  42.         }
  43.  
  44.    $conn = null;    
  45.  
  46.     header('location:home.php');
  47.  
  48.     exit(); 
  49.  
  50. ?>
  51.  
Not able to insert post-id ( pid ) and user-id ( id ) into comment's table . What am i missing?
Do help to solve this ?
Jan 29 '13 #1
13 10668
Rabbit
12,516 Expert Mod 8TB
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.
Jan 29 '13 #2
yateesh
36
@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 ?
Jan 29 '13 #3
Rabbit
12,516 Expert Mod 8TB
Your forgot to echo your variable.
Jan 29 '13 #4
yateesh
36
Could u tell me where is that ?
Jan 29 '13 #5
Rabbit
12,516 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. <input type="hidden" name="pid" value="<? echo $row['pid'];?>"/>
  2. <input type="hidden" name="id" value="<? echo $row['id'];?>"/>
Jan 29 '13 #6
yateesh
36
That is not working .
Jan 30 '13 #7
Rabbit
12,516 Expert Mod 8TB
Please post your full post.php code along with the html that the client sees.
Jan 30 '13 #8
yateesh
36
here is post.php

Expand|Select|Wrap|Line Numbers
  1.  
  2. <!DOCTYPE html>
  3.  
  4. <head>
  5.  
  6. <link href="css/reset.css" rel="stylesheet"/>
  7. <link href="css/post.css" rel="stylesheet"/>
  8.  
  9.  
  10. </head>
  11.  
  12. <body>
  13.     <div id="wrapper">
  14.     <div id="disp">
  15.     </div>
  16.         <header>
  17.  
  18.             <div id="login">
  19.  
  20.                 <ul>
  21.                     <li class="logout"><a class="button lightbox" href="logout.php">Log Out</a></li>
  22.                 </ul>
  23.             </div>
  24.  
  25.             <a style="position:relative;" class="logotext" href="home.php"><span>beez</span>treat</a>
  26.  
  27.             <input id="search" type="search" placeholder="Who likes to rock the party ?" autocomplete="off">
  28.  
  29.         </header>
  30.  
  31.         <nav id="top">
  32.             <ul>
  33.                 <li><a href="#">Home</a></li>/
  34.                 <li class="current"><a href="home.php">Menu 1</a></li>/
  35.                 <li><a href="#">Menu 2</a></li>/
  36.                 <li><a href="#">Menu 3</a></li>/
  37.                 <li class="last"><a href="#">Menu 4</a></li>
  38.             </ul>    
  39.         </nav>
  40.  
  41.         <div id="box">
  42.             <h1 class="title"><span class="landmark"></span></h1>
  43.             <p class="address"></p>
  44.         </div>
  45.  
  46.         <nav>
  47.             <form action="comment.php" method="post">
  48.         <input type="hidden" name='pid' value="<? echo $row['pid']); ?>" />
  49.                 <input type="hidden" name="id" value="<? echo $row['id'];?>"/>
  50.                 <input type="text" name="comment" value=""/>
  51.                 <input type="submit" name="submit" Value="Submit"/>
  52.             </form>
  53.         <nav>
  54.  
  55.         <footer>
  56.             <p> Copyright @ 2013 . All Rights Reserved . </p>
  57.             <p class="list">Enter your business here:<a href="#formi" class="lightbox">Add Listing</a> </p>
  58.         </footer>
  59.  
  60.  
  61.     </div>    
  62.     <?php include('postfunc.php'); ?>    
  63. </body>
  64.  
  65. </html>
  66.  
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 .
Jan 30 '13 #9
Rabbit
12,516 Expert Mod 8TB
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.
Jan 30 '13 #10
yateesh
36
I have displayed all the codes to you .

The below code is to display all the posts from database in home.php .
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     // Connection data (server_address, database, username, password)
  4.     $dbhost = 'localhost';
  5.     $dbname = 'project';
  6.     $dbuser = 'root';
  7.     $dbpass = '';
  8.  
  9.     // Display message if successfully connect, otherwise retains and outputs the potential error
  10.  
  11.     try {
  12.             $conn = new PDO("mysql:host=$dbhost; dbname=$dbname", $dbuser, $dbpass);
  13.             //echo 'Connected to database';
  14.             $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  15.     }
  16.  
  17.     catch(PDOException $e) 
  18.         {
  19.             echo $e->getMessage();
  20.         }
  21.  
  22.  
  23.     try 
  24.         {
  25.             $query = $conn->prepare("SELECT * FROM post ORDER by pid DESC limit 0,5");
  26.             $query->execute(); 
  27.         }
  28.  
  29.     catch (PDOException $e) 
  30.         {
  31.             error_log($e->getMessage());
  32.             die($e->getMessage());
  33.         }    
  34.  
  35.  
  36.     while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
  37.     echo    "<article id='display'>";
  38.  
  39.     echo    "<div class='item'>";
  40.  
  41.     echo    "<a href='#' >";
  42.  
  43.     echo    "</a>";
  44.  
  45.     echo    "<div class='content'>";
  46.  
  47.     echo    "<div class='details'></div>";
  48.  
  49.     echo    "<h2><a href='post.php?pid=".$row['pid']."' class='title'>"
  50.             .$row['title']."</a></h2>";
  51.  
  52.  
  53.  
  54.     echo    "<h6 class='single'><a href='post.php?pid=".$row['pid']."'>"
  55.             .$row['area']."</a></h6>";
  56.  
  57.     $data=$row['address'];
  58.  
  59.     $data1=myTruncate($data, 100, ' ');
  60.  
  61.  
  62.  
  63.     echo    "<p class='review'>".$data1."</p><a  href='post.php?pid="
  64.     .$row['pid']."  '
  65.     class='readmore'>Read More &rarr;</a>";
  66.  
  67.  
  68.  
  69.     echo    "<div id='comment' style='border:1px solid #cacaca;width:200px;
  70.     height:300px;display:none;'>";
  71.     echo    "</div>";
  72.  
  73.  
  74.     echo    "</div>";
  75.     echo    "</div>";
  76.     echo    "</div>";
  77.  
  78.     echo    "<article>";
  79.     }
  80.  
  81. $conn = null;
  82. ?>
  83.  
  84.  
  85.  
Thats it . No more codes are left .

I hope now you could find me the solution .
Jan 30 '13 #11
Rabbit
12,516 Expert Mod 8TB
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.
Jan 30 '13 #12
yateesh
36
Okay . What you say is right . But how do i pass them as you said from page to page ?
Jan 31 '13 #13
Rabbit
12,516 Expert Mod 8TB
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.
Jan 31 '13 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: pablo | last post by:
Dear News Groupers, I'am trying to send a php array with a hidden input field from a form to another script. The array is NOT made directly by way of <input name="arrayname" />. The array is...
9
by: Randell D. | last post by:
Folks, I have a large amount of values to store (we're talking tens, if not hundreds of bytes). I need this for a client side application - ignore the security consequences for the moment -...
6
by: Dan | last post by:
Is there a way to access and set a hidden input types in the code-behind page for ASP.Net? This seems like a common thing that would need to be done, I must be missing something obvious. ...
4
by: Alex | last post by:
Is there any way to delete the value of a html hidden input field from code-behind? I use an html hidden input to store an array of data. I collect this data using Request.Form or Request.Params....
2
by: UJ | last post by:
I have an aspx page that does some database look ups on the initial load. I then need to do stuff later after an autopostback based on some of the values. So I put some variables in input variables...
2
by: Navillus | last post by:
Hi all, I'm trying to insert a javascript variable into a hidden input form field. Here's what it looks like: <form name="loginForm" action="scripts/wgate/ziac_login/!?~language=EN"> <input...
4
by: WB | last post by:
Hi, I need to validate a hidden input in my webform to ensure that it's got value. The controls look something like this: <input id="HidSelectedStates" type="hidden" runat="server" /><br...
4
by: Bosconian | last post by:
I've been fighting with this for an hour. My form contains a hidden input with the value initially set to "". When a user clicks on the link, a function is called that updates the hidden form...
5
by: bucchi | last post by:
Hi, I am doing the following in a javascript function: var hiddenElement = document.createElement("input"); hiddenElement .setAttribute("type", "hidden"); hiddenElement...
3
by: zac540 | last post by:
Hey everyone! First of all I'd like to say that I did my best to look for any other relevant posts. The best I found was this interesting thread.. http://bytes.com/forum/thread594982.html If...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.