Forum Moderators: coopster

Message Too Old, No Replies

Passing variables

         

dave1236

1:26 am on Jun 29, 2007 (gmt 0)

10+ Year Member



All:

I am having an issue with properly passing a variable between pages...

I have the following setup...

1) a homepage with a list of articles, identified by an id. I pass the value to page 2 by in the following manner (this works):
a href='page2.php?id=$id'>Comment</a>

2) On page2.php, I return all comments made by users the relate to article #1. This works fine as well. I accomplish this as follows (condensed):

<?php
$id = $_GET['id'];
$sql = "SELECT * FROM Comments WHERE id='$id'";
while ($row = mysql_fetch_array($result))
{
extract($row);
echo "
<tr>\n
<td>$comments</td>\n
<td>$id</td>\n
</tr>";
}
?>

So, I search my DB for all comments relating to the selected id. and I print the id in a column.

3) At the bottom of page2.php, I provide a comment box for people to add their own feedback. I have everything working, as it populates the database properly, EXCEPT I cannot seem to get the value for id to be saved/passed to the for that inserts the comment

For example, i want to echo the value for id in an input box, but nothing seems to work.

For example...

<form action="postcomment.php" method="POST">
<td> <select name="source" id="source">
<option value="na">zero</option>
<option value="one">one</option>
<option value="two">two</option>
</select> </td></tr>
<tr><td align="right"><b>Comment</b></td>
<td><input type="text" name="comments"
value"<?php echo @$comments?>"
></td>
THIS IS WHERE I HAVE ISSUES--->
<tr><td>id</td>
<td><input type="int" name="id2"
value
"<?php echo $id?>" <-- This part does show the proper id #
<br>

my insert query on the postcomment.php page:

$sql = "INSERT INTO Comments (datetime,id,source,comments) VALUES
('$today','$id2','$source','$comments')";

Thanks for comments...

dreamcatcher

8:28 am on Jun 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



input type="text" name="id2"

// Apply mysql_real_escape_string [uk2.php.net] to post vars..
$_POST = array_map('mysql_real_escape_string',$_POST);

$sql = "INSERT INTO Comments (datetime,id,source,comments) VALUES
('{$_POST['today']}','{$_POST['id2']}','{$_POST['source']}','{$_POST['comments']}')";

dc

dave1236

1:37 pm on Jun 29, 2007 (gmt 0)

10+ Year Member



Dreamcatcher:

Thanks for your help!

That works exactly the right way. I appreciate your help.

dreamcatcher

2:44 pm on Jun 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad to help. :)

dc