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
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.