Jump to content

[SOLVED] Issue with GET


ip2g

Recommended Posts

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 :P

Link to comment
https://forums.phpfreaks.com/topic/144557-solved-issue-with-get/
Share on other sites

  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 :P

 

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

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);

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);
		}
	?>

 

 

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

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.