ip2g Posted February 10, 2009 Share Posted February 10, 2009 Okay I'm pretty noobish to PHP, and I have no idea why this isn't working. I thought it would work, but it doesn't. Okay so let me explain before I post the code. On index.php, I show the most recent blog posts (it works fine, don't worry about that) as a link. Then a form sends it to the post.php page using GET (so they can save the page). The problem is well... on post.php, nothing happens. It somehow doesn't receive the information. I have a feeling it's a really noobish problem, but all the same, be nice. Okay so here are the codes: index.php <? $dbhost = 'localhost'; $dbuser = 'ip2g_*****'; $dbpass = '*****'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'ip2g_*****'; mysql_select_db($dbname) or die('Could not connect to Database'); $i = 1; for ($i=1;$i<=3;$i++) { $query = "SELECT * from Posts WHERE postID = '$i'"; $result = mysql_query($query,$conn) or die(mysql_error()); $row = mysql_fetch_assoc($result); $postTitle = $row['postTitle']; $postDate = $row['postDate']; echo " <li class='menu'> <a> <form action='post.php' method='GET'> <input type='hidden' name='postID' value='$postID' /> <span class='name'>" . $postTitle . "</span> <span class='comment'>" . $postDate . "</span> <span class='arrow'></span> </form> </a> </li>"; } ?> post.php <? $dbhost = 'localhost'; $dbuser = 'ip2g_*****'; $dbpass = '*****'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'ip2g_*****'; mysql_select_db($dbname) or die('Could not connect to Database'); $query = "SELECT * from Posts WHERE postID = \"{_GET['postID']}\""; $result = mysql_query($query,$conn) or die(mysql_error()); $row = mysql_fetch_assoc($result); $postPost = $row['postPost']; $postDate = $row['postDate']; $postTitle = $row['postTitle']; echo " <li class='menu'> <a> <span class='name'>" . $postTitle . "</span> <span class='comment'>" . $postDate . "</span> <span class='arrow'></span> </a> </li>"; ?> Right now, it's not supposed to print out the post, it's just supposed to print out the title and the date. But, once again, it won't print out anything. It obviously has something to do with the form and the query: ($query = "SELECT * from Posts WHERE postID = \"{_GET['postID']}\"" Once again, I be a nub, so help me Quote Link to comment https://forums.phpfreaks.com/topic/144557-solved-issue-with-get/ Share on other sites More sharing options...
jeger003 Posted February 10, 2009 Share Posted February 10, 2009 Quote Okay I'm pretty noobish to PHP, and I have no idea why this isn't working. I thought it would work, but it doesn't. Okay so let me explain before I post the code. On index.php, I show the most recent blog posts (it works fine, don't worry about that) as a link. Then a form sends it to the post.php page using GET (so they can save the page). The problem is well... on post.php, nothing happens. It somehow doesn't receive the information. I have a feeling it's a really noobish problem, but all the same, be nice. Okay so here are the codes: index.php <? $dbhost = 'localhost'; $dbuser = 'ip2g_*****'; $dbpass = '*****'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'ip2g_*****'; mysql_select_db($dbname) or die('Could not connect to Database'); $i = 1; for ($i=1;$i<=3;$i++) { $query = "SELECT * from Posts WHERE postID = '$i'"; $result = mysql_query($query,$conn) or die(mysql_error()); $row = mysql_fetch_assoc($result); $postTitle = $row['postTitle']; $postDate = $row['postDate']; echo " <li class='menu'> <a> <form action='post.php' method='GET'> <input type='hidden' name='postID' value='$postID' /> <span class='name'>" . $postTitle . "</span> <span class='comment'>" . $postDate . "</span> <span class='arrow'></span> </form> </a> </li>"; } ?> post.php <? $dbhost = 'localhost'; $dbuser = 'ip2g_*****'; $dbpass = '*****'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'ip2g_*****'; mysql_select_db($dbname) or die('Could not connect to Database'); $query = "SELECT * from Posts WHERE postID = \"{_GET['postID']}\""; $result = mysql_query($query,$conn) or die(mysql_error()); $row = mysql_fetch_assoc($result); $postPost = $row['postPost']; $postDate = $row['postDate']; $postTitle = $row['postTitle']; echo " <li class='menu'> <a> <span class='name'>" . $postTitle . "</span> <span class='comment'>" . $postDate . "</span> <span class='arrow'></span> </a> </li>"; ?> Right now, it's not supposed to print out the post, it's just supposed to print out the title and the date. But, once again, it won't print out anything. It obviously has something to do with the form and the query: ($query = "SELECT * from Posts WHERE postID = \"{_GET['postID']}\"" Once again, I be a nub, so help me try change ($query = "SELECT * from Posts WHERE postID = \"{_GET['postID']}\"" to ($query = "SELECT * from Posts WHERE postID = \"{$_GET['postID']}\"" just added the $ sign to $_GET Quote Link to comment https://forums.phpfreaks.com/topic/144557-solved-issue-with-get/#findComment-758598 Share on other sites More sharing options...
ip2g Posted February 10, 2009 Author Share Posted February 10, 2009 Wow I can't believe I forgot that. But still, it won't print out anything. Is it the placement of the form tag in index.php? Quote Link to comment https://forums.phpfreaks.com/topic/144557-solved-issue-with-get/#findComment-758599 Share on other sites More sharing options...
jeger003 Posted February 10, 2009 Share Posted February 10, 2009 Quote Wow I can't believe I forgot that. But still, it won't print out anything. Is it the placement of the form tag in index.php? try adding error_reporting and see if you get any errors also make sure your form is able to find post.php and test that as way.......like have it only $_Get post id and echo it so that you know the query is running. ini_set('error_reporting', E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/144557-solved-issue-with-get/#findComment-758608 Share on other sites More sharing options...
ip2g Posted February 11, 2009 Author Share Posted February 11, 2009 Hmm, well I did the ini_set('error reporting') thing and I found the error to be that I didn't initialize $postID. But even then, it still does not print out anything on the other page, and the other page literally says: <? $a = $_GET['postID']; echo $a; ?> What's up now? index.php: <? $dbhost = 'localhost'; $dbuser = 'ip2g_*****'; $dbpass = '*****'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'ip2g_*****'; mysql_select_db($dbname) or die('Could not connect to Database'); $num = mysql_query("SELECT * FROM Posts"); $num_rows = mysql_num_rows($num); for ($i=$num_rows;$i>=($num_rows - 2);$i--) { $query = "SELECT * from Posts WHERE postID = '$i'"; $result = mysql_query($query,$conn) or die(mysql_error()); $row = mysql_fetch_assoc($result); $postTitle = $row['postTitle']; $postDate = $row['postDate']; $postID = $row['postID']; echo " <li class='menu'> <form action='post.php' method='GET'> <a href='post.php'> <input type='hidden' name='postID' value='$postID' /> <span class='name'>" . $postTitle . "</span> <span class='comment'>" . $postDate . "</span> <span class='arrow'></span> </a> </form> </li>"; ini_set('error_reporting', E_ALL); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/144557-solved-issue-with-get/#findComment-759347 Share on other sites More sharing options...
gevans Posted February 11, 2009 Share Posted February 11, 2009 <form action='post.php' method='GET'> <a href='post.php'> <input type='hidden' name='postID' value='$postID' /> <span class='name'>" . $postTitle . "</span> <span class='comment'>" . $postDate . "</span> <span class='arrow'></span> </a> </form> should be; <form action='post.php' method='GET'> <input type="submit" value="submit" name="submit" /> <input type='hidden' name='postID' value='$postID' /> <span class='name'>" . $postTitle . "</span> <span class='comment'>" . $postDate . "</span> <span class='arrow'></span> </form> Quote Link to comment https://forums.phpfreaks.com/topic/144557-solved-issue-with-get/#findComment-759349 Share on other sites More sharing options...
ip2g Posted February 11, 2009 Author Share Posted February 11, 2009 I would like to do this without using submit buttons though... Is that possible? Quote Link to comment https://forums.phpfreaks.com/topic/144557-solved-issue-with-get/#findComment-759351 Share on other sites More sharing options...
gevans Posted February 11, 2009 Share Posted February 11, 2009 use javascript with the anchor tag to submit the form Quote Link to comment https://forums.phpfreaks.com/topic/144557-solved-issue-with-get/#findComment-759352 Share on other sites More sharing options...
ip2g Posted February 11, 2009 Author Share Posted February 11, 2009 Thanks! I never really thought about using javascript in this situation. EDIT: Oh wait.. it only worked like once.. how is that possible? Quote Link to comment https://forums.phpfreaks.com/topic/144557-solved-issue-with-get/#findComment-759357 Share on other sites More sharing options...
PFMaBiSmAd Posted February 11, 2009 Share Posted February 11, 2009 If your form has no user input fields and is only sending the postID=$postID as a GET field, just make the link have the postID as a parameter on the end of the URL. Quote Link to comment https://forums.phpfreaks.com/topic/144557-solved-issue-with-get/#findComment-759360 Share on other sites More sharing options...
ip2g Posted February 11, 2009 Author Share Posted February 11, 2009 So change it to <form id='submit' action='post.php?postID=$postID' method='GET'>? Quote Link to comment https://forums.phpfreaks.com/topic/144557-solved-issue-with-get/#findComment-759361 Share on other sites More sharing options...
gevans Posted February 11, 2009 Share Posted February 11, 2009 What PF is saying is you don't need a form, and he's very right; echo " <li class='menu'> <a href='post.php?postID=$postID'> <span class='name'>" . $postTitle . "</span> <span class='comment'>" . $postDate . "</span> <span class='arrow'></span> </a> </li> Quote Link to comment https://forums.phpfreaks.com/topic/144557-solved-issue-with-get/#findComment-759362 Share on other sites More sharing options...
ip2g Posted February 11, 2009 Author Share Posted February 11, 2009 Oh okay, I never knew that. Thanks for all the help guys! And thanks for putting up with my noobish ways, I pretty much just started PHP. I'm more of a C++ guy Quote Link to comment https://forums.phpfreaks.com/topic/144557-solved-issue-with-get/#findComment-759368 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.