PHP help, sending variable to next page??

varmit

macrumors 68000
Original poster
Aug 5, 2003
1,830
0
I guess I have just either made my page just a little too simple it complicates things, or I just don't know about something. What this script does is it calls to my Blog database and displays all the dates for when I have something posted. From there, I want people to click on the date to then view the entry. But I'm just not sure how to pass the date to the next php page so the info can be displayed.

PHP:
<?php
$conn = mysql_connect("localhost", "read", "");
mysql_select_db("Blog",$conn);
$sql = "SELECT ID, date_format(date, '%b %e, %Y at %r') as fmt_date, text, title FROM blogTable order by date desc";
$result = mysql_query($sql, $conn) or die(mysql_error());
while ($newArray = mysql_fetch_array($result)) {
	$date = $newArray['fmt_date'];
	echo "<a href=\"$date\" target=\"_blank\">$date</a><br><br>";
}
?>
 

4409723

Suspended
Jun 22, 2001
2,222
0
Looking just at the echo line and presuming you have collected the data correctly, I see what I believe to be a mistake in the echo line:

echo "<a href=\"" .$date. "\" target=\"_blank\"> " .$date. "</a><br><br>";

I think that *might* do it. You are using the slashes to 'ignore' the variables you want to print.

I'm sure somebody will give you the full solution soon but this could point you in the right direction in the meantime. Rather than me messing around with colour tags I used Subethaedit and made a PDF to show the error using syntax highlighting.
 

Attachments

Knox

Administrator
Staff member
Jul 1, 2002
1,267
1
UK
Wes said:
Looking just at the echo line and presuming you have collected the data correctly, I see what I believe to be a mistake in the echo line:

echo "<a href=\"" .$date. "\" target=\"_blank\"> " .$date. "</a><br><br>";

I think that *might* do it. You are using the slashes to 'ignore' the variables you want to print.
Both ways are valid actually - the slashes escape the quotes, not the variables. Varmit's way does have some limitations however (not able to access associative arrays, nor objects within objects) but for simple things i tend to use it.

To pass a value to another page via a link like this you need to assign them to a variable, for example

<a href="next.php?date=20050417">20050417</a>

On the next page page you can then access the variable using $_GET["date"]. You should always check that the variable contains data that you're expecting, for example, in the above case it should only contain numbers and not letters.

http://www.php.net/manual/en/security.variables.php
 

varmit

macrumors 68000
Original poster
Aug 5, 2003
1,830
0
This is the code for the first page, and it will display all the dates that I have posted something. Then when I click on a date, it goes to the next page and transfers the ID tag for the entry.
PHP:
<?php
$conn = mysql_connect("localhost", "read", "");
mysql_select_db("Blog",$conn);
$sql = "SELECT ID, date_format(date, '%b %e, %Y at %r') as fmt_date FROM blogTable order by date desc";
$result = mysql_query($sql, $conn) or die(mysql_error());
while ($newArray = mysql_fetch_array($result)) {
	$date = $newArray['fmt_date'];
	$id = $newArray['ID'];
	echo "<a href=\"day.php?ID=$id\">$date</a><br><br>";
}
?>
But then I get this error.

"You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"

With this code.

PHP:
<?php
$i = $_Get["ID"];
$conn = mysql_connect("localhost", "read", "");
mysql_select_db("Blog",$conn);
$sql = "SELECT ID, date_format(date, '%b %e, %Y at %r') as fmt_date, text, title FROM blogTable WHERE ID = $i";
$result = mysql_query($sql, $conn) or die(mysql_error());
$newArray = mysql_fetch_array($result);
$day = $newArray['fmt_date'];
$text = $newArray['text'];
$title = $newArray['title'];
echo "<table width=\"525\" cellpadding=\"2\" cellspacing=\"1\" boarder=\"1\" bordercolor=\"#cccccc\">
  	  <tr>
    	  <td bgcolor=\"#cccccc\"><b>$title</b><br><i>$day</i></td>
  	  </tr>
  	  <tr>
    	  <td>$text</td>
  	  </tr>
      </table><br>"
?>
I believe I narrowed the error to the WHERE statement and the equal sign that I'm using. I'll see what I can do from there. I can understand you worry about security, thats why I use the read user name with a blank password, its a user with only read ability, same with my write user. The root user name to the databases has a very complicated password.
 

varmit

macrumors 68000
Original poster
Aug 5, 2003
1,830
0
Hold it, is there no WHERE statement in PHP? I mean, I can't find any documentation on it to see if I'm doing it right, and all my books don't have a WHERE statement for PHP. How the hell am I suppost to pick out a single record without a WHERE statement? Better stated, whats the equivalant that I can use?
 

Knox

Administrator
Staff member
Jul 1, 2002
1,267
1
UK
varmit said:
Hold it, is there no WHERE statement in PHP? I mean, I can't find any documentation on it to see if I'm doing it right, and all my books don't have a WHERE statement for PHP. How the hell am I suppost to pick out a single record without a WHERE statement? Better stated, whats the equivalant that I can use?
I think you're mixing up two languages there, the database query (what you have in the $sql variable) is written in SQL, a database language. PHP simply provides an interface to the specific type of SQL server, in this case MySQL.

You may want to change the query to the following -

PHP:
$sql = "SELECT ID, date_format(date, '%b %e, %Y at %r') as fmt_date, text, title FROM blogTable WHERE ID = ".intval($i);
The problem could be that either $i is blank, or it contains non-numeric characters in it. You either need to enclose a variable in quotes (" or ', doesn't matter) or make sure it is an integer.

As a side note - I tend to format the date within PHP, rather than formatting it using the database (see the strtotime() and date() php functions), and I would avoid using $i as a variable name unless it's within a for loop, just for readability
 

varmit

macrumors 68000
Original poster
Aug 5, 2003
1,830
0
Well, its either the " echo "<a href=\"day.php?ID=$id\">$date</a><br> $id<br><br>";" on the first page that isn't giving the variable ID the number. When viewing that page, the ID=$id is showing up with $id having the id number of the post, so that seems fine but I'm not sure.

Then comes the next page with the $i = $_GET["ID"], which either the ID number is blank, but the URL at the tip had ID being correct, or the GET function isn't working.

http://www.ducktapeandglue.com/pages/fullupdatecopy.php is the URL, and you can see I echoed the ID. Then choosing a date ends up on a page with no info filled in.
 

gonz

macrumors newbie
Apr 17, 2005
7
0
varmit said:
Well, its either the " echo "<a href=\"day.php?ID=$id\">$date</a><br> $id<br><br>";" on the first page that isn't giving the variable ID the number. When viewing that page, the ID=$id is showing up with $id having the id number of the post, so that seems fine but I'm not sure.

Then comes the next page with the $i = $_GET["ID"], which either the ID number is blank, but the URL at the tip had ID being correct, or the GET function isn't working.
I'm asuming you're using PHP >4.1...

You can try and just dump $_GET:
Code:
var_dump($_GET);
then you'll see if there's any data in the $_GET variable at all...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.