469,352 Members | 1,945 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

How do i transfer $_GET['id'] ?

i need to get the value of the id from 1 page called image.php a insert it in to the database from another page submitComment.php

this is my code....

image.php....

Expand|Select|Wrap|Line Numbers
  1. $photoId = mysql_escape_string($_GET['id']);                
  2. $q = "SELECT * FROM comments WHERE photoId = '" . $photoId . "' ORDER BY date DESC LIMIT 5";
submitComment.php....

Expand|Select|Wrap|Line Numbers
  1. $theMessage = addslashes(strip_tags($_POST['message'])); 
  2. $q = "INSERT INTO comments (photoId, date, content) VALUES ('" . $photoId . "', '" . $theDate . "', '" . $theMessage . "')";
  3. $result = $mysqli->query($q) or die ('error could not insert into comments' . mysql_error());
everything works fine except i get this error...

Undefined variable: photoId in C:\wamp\www\fotostor\submitComment.php on line 39

and the photoId in the database displays 0, i cant merge the pages togetherso it recognises the image.php's $_GET['id']; so i was wondering if i could get the value of it and transfer it some how, please help
Aug 12 '09 #1

✓ answered by bibiki

thanks man. however, i am not sure if we are both pounding on the same problem here. what i am trying to contribute to is luke_noob's problem about passing variables without a form. I strugled with that question, too, and I was answered yesterday by a moderator nicknamed Atli. So, I think I got it good this time.
I would not like to take up your time trying to teach you (a moderator) what you already know. However, I would like to add the following.
The value of $_GET['id'] is taken from the URL of the page this ($_GET['id']) script is executed in. In other words, the initial page that linkes to the script page holding the $_GET script must have something like:
<a href="this_script.php?id=<?php echo 'whatever_the_id_is'?>"
Now, the $_GET takes the id from the URL - www.this_script.php?id=<?php echo 'whatever_the_id_is'?>. Then you use the mysql_escape_string($_GET['id']) to clear off any unwanted symbol so that you get only the result of " <?php echo 'whatever_the_id_is'?> "
THAT'S IT. All Luke_noob must pay attantion to is the nature of $_GET method. What it does is take variable values from the URL it is executed in. This did it for me. Atli explained it for me and I thought I should pass it on to Luke_noob too. I hope that helps everybody.

14 8501
dlite922
1,584 Expert 1GB
how are users getting to submitComment.php from image.php?

link, form button?

Then change it to add the photoID as a URL parameter. Either in the link or the forum action.

You can also use session, but not recommended for your problem here.




Dan
Aug 12 '09 #2
sorry Its quite hard to explain but basically i need to get the page id value from one page and store it in to a varable which i can do. but then i need to somehow send that varible information to another page and from that page send it to my database.

on the submitComment.php there is a comment form that posts the comment and at the same time stores the information into the database, the problem is that it doesnt insert the photoId (the image.php $_GET['id']) because it dosent recognise it. so i was think or some how sending the id (image.php's $_GET['id']) to the submitComments page so it does.


but how can i send the variable? without form or button? thanx in advance, iv been googling for days
Aug 12 '09 #3
dlite922
1,584 Expert 1GB
post the entire code from both pages and I'll try to take a look at it for you.
Aug 12 '09 #4
submitComment.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. require 'database.php';
  4. require 'functions.php';
  5.  
  6. $timestamp = time();
  7. $date_time_array = getdate($timestamp);
  8.  
  9. $hours = $date_time_array['hours'];
  10. $minutes = $date_time_array['minutes'];
  11. $seconds = $date_time_array['seconds'];
  12. $month = $date_time_array['mon'];
  13. $day = $date_time_array['mday'];
  14. $year = $date_time_array['year'];
  15. ?>
  16. <?php
  17.  
  18. $timestamp = mktime($hours + 8,$minutes,$seconds,$month,$day,$year);
  19. $theDate = strftime('%Y-%m-%d %H:%M:%S',$timestamp);
  20.  
  21. mysql_connect("localhost","root",""); 
  22.  
  23. mysql_select_db("myPhotos") or die("Unable to select database");
  24.  
  25. $theMessage = addslashes(strip_tags($_POST['message']));
  26.  
  27. $photoId = mysql_escape_string($_GET['id']);              
  28.  
  29. $q = "INSERT INTO shoutbox (photoId, date, content) VALUES ('" . $photoId . "', '" . $theDate . "', '" . $theMessage . "')";
  30. $result = $mysqli->query($q) or die ('error could not insert into shout box' . mysql_error());  
  31.  
  32. $theMessage = str_replace(":frown:","&nbsp;<img src='smilies/frown.gif'>", $theMessage); 
  33.  
  34. ?>
  35.         <div class="comment-unapproved">
  36.             <div class="date">
  37.                 <span class="day-month"><?php echo date('d M'); ?></span>
  38.                 <span class="year"><?php echo date('y'); ?></span>
  39.             </div>
  40.             <span class="content"><?php echo stripslashes($theMessage); ?></span>
  41.             <div class="clear"></div>
  42.         </div>



review_image.php

Expand|Select|Wrap|Line Numbers
  1. <body>  
  2.   <?php 
  3.     require 'functions.php';
  4.   ?>       
  5.  
  6.       <div id="container">
  7.            <div id="main">
  8.  
  9.     <img src="images/logos/logofold.jpg" width="64" height="126" class="logofold">        
  10.     <img src="images/logos/logo1.jpg" width="913" height="126" id="logo">
  11.      <div class="aerobuttonmenu">
  12.  
  13. <a href="#" class="aero"><span>About Us</span></a> 
  14. <a href="#" class="aero"><span>Services</span></a>
  15. <a href="contact.html" class="aero"><span>Contact</span></a> 
  16. <a href="index.php" class="aero"><span>Home</span></a> 
  17.  
  18. </div></div></div>
  19.  
  20.       <div id="container"> 
  21.       <img src="images/logos/footer1.jpg" width="100%" height="31" align="right" id="footer1">
  22.  
  23.          <p id="text"><font color="#486583">Photo Description</font></p>
  24.                           </div> </div>
  25.  
  26.        <div id="container">
  27.            <div id="main">
  28.  
  29. <?php getChosenPhoto($_GET['id']);  ?>
  30. <table align="center"><tr><td>  
  31. <div id="container">
  32.     <div id="comments">
  33. <?php
  34.  
  35. include("database.php");
  36.  
  37.    function datediff($d1, $d2)
  38.  {
  39.     $d1 = (is_string($d1) ? strtotime($d1) : $d1);
  40.     $d2 = (is_string($d2) ? strtotime($d2) : $d2);
  41.  
  42.     $diff_secs = abs($d1 - $d2);
  43.     $base_year = min(date("Y", $d1), date("Y", $d2));
  44.  
  45.     $diff = mktime(0, 0, $diff_secs, 1, 1, $base_year);
  46.     $diffArray = array(
  47.         "years" => date("Y", $diff) - $base_year,
  48.         "months_total" => (date("Y", $diff) - $base_year) * 12 + date("n", $diff) - 1,
  49.         "months" => date("n", $diff) - 1,
  50.         "days_total" => floor($diff_secs / (3600 * 24)),
  51.         "days" => date("j", $diff) - 1,
  52.         "hours_total" => floor($diff_secs / 3600),
  53.         "hours" => date("G", $diff),
  54.         "minutes_total" => floor($diff_secs / 60),
  55.         "minutes" => (int) date("i", $diff),
  56.         "seconds_total" => $diff_secs,
  57.         "seconds" => (int) date("s", $diff)
  58.     );
  59.     if($diffArray['days'] > 0){
  60.         if($diffArray['days'] == 1){
  61.             $days = '1 day';
  62.         }else{
  63.             $days = $diffArray['days'] . ' days';
  64.         }
  65.         return $days . ' and ' . $diffArray['hours'] . ' hours ago';
  66.     }else if($diffArray['hours'] > 0){
  67.         if($diffArray['hours'] == 1){
  68.             $hours = '1 hour';
  69.         }else{
  70.             $hours = $diffArray['hours'] . ' hours';
  71.         }
  72.         return $hours . ' and ' . $diffArray['minutes'] . ' minutes ago';
  73.     }else if($diffArray['minutes'] > 0){
  74.         if($diffArray['minutes'] == 1){
  75.             $minutes = '1 minute';
  76.         }else{
  77.             $minutes = $diffArray['minutes'] . ' minutes';
  78.         }
  79.         return $minutes . ' and ' . $diffArray['seconds'] . ' seconds ago';
  80.     }else{
  81.         return 'Less than a minute ago';
  82.     }
  83.  }  
  84.  
  85. $timestamp = time();
  86. $date_time_array = getdate($timestamp);
  87.  
  88. $hours = $date_time_array['hours'];
  89. $minutes = $date_time_array['minutes'];
  90. $seconds = $date_time_array['seconds'];
  91. $month = $date_time_array['mon'];
  92. $day = $date_time_array['mday'];
  93. $year = $date_time_array['year'];
  94.  
  95. $timestamp = mktime($hours + 8,$minutes,$seconds,$month,$day,$year);
  96. $theDate = strftime('%Y-%m-%d %H:%M:%S',$timestamp);
  97.  
  98. mysql_connect("localhost","root",""); 
  99.  
  100. mysql_select_db("myphotos") or die("Unable to select database"); 
  101.  
  102. $photoId = mysql_escape_string($_GET['id']);
  103.  
  104. $q = "SELECT * FROM shoutbox WHERE photoId = '" . $photoId . "' ORDER BY date DESC LIMIT 5";
  105.  
  106. $result = $mysqli->query($q);
  107. while($row = $result->fetch_array()) {
  108.     $date = strtotime($row['date']);
  109.     $dayMonth = date('d M', $date);
  110.     $year = date('y', $date);                     
  111.     $message = $row['content'];
  112.     $datediff = datediff($theDate, $date);
  113.  
  114. $message = str_replace(":frown:","&nbsp;<img src='smilies/frown.gif'>", $message);      
  115. ?>    
  116.  
  117.         <div class="comment">
  118.             <div class="date">
  119.                 <span class="day-month"><?php echo $dayMonth; ?></span>
  120.                 <span class="year"><?php echo $year; ?></span>
  121.             </div>
  122.             <span class="content"><?php echo stripslashes($message);?> <span class="time"><?php echo $datediff; ?></span></span>
  123.             <div class="clear"></div>
  124.         </div>
  125. <?php 
  126. }
  127.  
  128.  
  129. ?>
  130.     </div>
  131.     <div id="submission">
  132.         <form name="comment-submission">
  133.  
  134. <a title="Frown"><img border="0" src="smilies/frown.gif" onclick="InsertWord(':frown:');" width="15" height="15"></a>
  135.  
  136.             <textarea id="message" name="message" value="message"></textarea>
  137.             <span class="limit">140</span>
  138.             <input type="submit" id="submit-comment" value="" />
  139.         </form>
  140.         <div class="clear"></div>
  141.     </div>
  142. </div>
Aug 12 '09 #5
dlite922
1,584 Expert 1GB
I think I know what you need.

In review_image you have this $photo_id that you use to pull an image. Then if someone wants to make a comment, they fill out this form:

<form name="comment-submission"> on like 132 above.

This message and comment should be attached to that photo_id, which needs to get passed to submitComment.php

Well, just like how you're sending the message itself, send the photo_id, by putting it in a hidden field inside that form.

One thing I don't see is how you get to submitComment.php. Your form doesn't have an action="" that goes to this page.

A hidden field is like
Expand|Select|Wrap|Line Numbers
  1. <input type="hidden" value="<?php echo $photo_id;?>" />
Does that help?



Dan
Aug 12 '09 #6
dlite922
1,584 Expert 1GB
Forgot to mention, when you put it in a hidden field, you grab it just like you grab message, via $_POST. make sure you still run mysql_escape on it because just because it's hidden, doesn't mean people can't change the value.


Dan
Aug 12 '09 #7
i tryed this but i get an error saying Undefined index: photo_Id in C:\wamp\www\fotostor\submitComment.php

submitComment.php

Expand|Select|Wrap|Line Numbers
  1. $theMessage = addslashes(strip_tags($_POST['message']));
  2.  
  3. $photoId = mysql_escape_string($_POST['photo_Id']);
  4.  
  5. $q = "INSERT INTO shoutbox (photoId, date, content) VALUES ('" . $photoId . "', '" . $theDate . "', '" . $theMessage . "')";
  6. $result = $mysqli->query($q) or die ('error could not insert into shout box' . mysql_error());  
  7.  
review_image.php

Expand|Select|Wrap|Line Numbers
  1. <textarea id="message" name="message" value="message"></textarea>
  2.             <span class="limit">140</span>
  3.             <input type="hidden" id="photo_Id" value="<?php echo mysql_escape_string($photoId); ?>"  name="photo_Id">
  4.             <input type="submit" id="submit-comment" value="" />
  5.         </form>
  6.  
Aug 12 '09 #8
dlite922
1,584 Expert 1GB
is your form posting to that page?

i.e. ensure your form action is equal to "submitComment.php" and the method is equal to 'post'.

By the way, escape_string() is useless when you echo in the hidden field.

If you do the above it will work.



Dan
Aug 12 '09 #9
didnt work, thanx anyway for your help and time, i think i gonna up to be honest..
Aug 12 '09 #10
bibiki
6
i am a newbie myself, and am struggling with the exact same problem... so i thought i might offer some insight: ae you sure that your SQL statement of WHERE has the proper value. What I see you have is:
WHERE photoId = '" . $photoId . "
Try maybe:
"...... WHERE photold= 'photoId' ...." Note that I am specifically suggesting you take out the periods and double quotes.
Sep 12 '09 #11
Dormilich
8,652 Expert Mod 8TB
@bibiki
that would insert "photoId" as value, which is imo not intended.
Sep 13 '09 #12
bibiki
6
yeah... I c that I forgot the dollar sign. so my actual suggestion is that you put in '$photoId'
Sep 13 '09 #13
Dormilich
8,652 Expert Mod 8TB
both ways are valid. which one you use is a matter of personal preference.
Sep 13 '09 #14
bibiki
6
thanks man. however, i am not sure if we are both pounding on the same problem here. what i am trying to contribute to is luke_noob's problem about passing variables without a form. I strugled with that question, too, and I was answered yesterday by a moderator nicknamed Atli. So, I think I got it good this time.
I would not like to take up your time trying to teach you (a moderator) what you already know. However, I would like to add the following.
The value of $_GET['id'] is taken from the URL of the page this ($_GET['id']) script is executed in. In other words, the initial page that linkes to the script page holding the $_GET script must have something like:
<a href="this_script.php?id=<?php echo 'whatever_the_id_is'?>"
Now, the $_GET takes the id from the URL - www.this_script.php?id=<?php echo 'whatever_the_id_is'?>. Then you use the mysql_escape_string($_GET['id']) to clear off any unwanted symbol so that you get only the result of " <?php echo 'whatever_the_id_is'?> "
THAT'S IT. All Luke_noob must pay attantion to is the nature of $_GET method. What it does is take variable values from the URL it is executed in. This did it for me. Atli explained it for me and I thought I should pass it on to Luke_noob too. I hope that helps everybody.
Sep 13 '09 #15

Post your reply

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

Similar topics

2 posts views Thread by Ricki Susic | last post: by
reply views Thread by James | last post: by
3 posts views Thread by sharma | last post: by
27 posts views Thread by meltedown | last post: by
1 post views Thread by stephane | last post: by
12 posts views Thread by Lennart Anderson | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.