Page 1 of 1
Querystring query
Posted: Fri Mar 07, 2003 2:46 pm
by toms100
ok heres meine problem.
im using the following code to make a guest book. i can read from my data base but i wanted to keep the entire guestbook in one page. im using functions for writting too it and displaying it but i havnt made all of those yet.
Code: Select all
<?php
function DB() {
$db = mysql_connect("localhost", "root");
return $db;
}
function Gb_Read($Gb_MsgPerPage=20,$Gb_MsgMode=1) {
// Gb_MessageMode = 1 means that the guest book is displayed with post titles and you must click to see each post.
// If it = 0 then each post is is shown on the page in its entirity.
// $gb_MsgPerPage is the number of posts shown per page
$db = mysql_connect("localhost", "root");
mysql_select_db("gbook",DB());
$result = mysql_query("SELECT * FROM posts",DB());
print "<table border=1 ><tr><td>Messages</td></tr>";
$i = 1;
while ($gb = mysql_fetch_array($result)) {
do {
if ( $i > $Gb_MsgPerPage ) {
break; }
print
print "<tr><td>";
print ("<A href=gb.php?GbPage=GbViewPost&id=$gbїID]><B> $gbїTitle] </b> </a> ");
print ("<i>Posted by $gbїPoster]");
print ("On $gbїDate]</i>");
print ("</tr></td>");
$i++;
} while ($gb = mysql_fetch_array($result));
}
print ("</table>");
}
$GbPage = $HTTP_GET_VARSї'GbPage'];
if ($GbPage) {
Switch ($GbPage) {
// Displays info based on the querystring result. This is the meat of the script
case GbViewPost:
echo "View post with ID";
break;
}
}
else {
Gb_Read();
}
?>
basically as the code runs i get this error which is refering to the $GbPage = $HTTP_GET_VARS['GbPage']; line. i
Notice: Undefined index: GbPage in E:\Tom\php work\guestbook\gb.php on line 38
dont get this error when the querystring has a value. how do i stop this error occuring (mainly the message) as the code works fine just that message keeps coming up..
many thanks
Tom
?>
Posted: Fri Mar 07, 2003 3:18 pm
by McGruff
Don't meant to be a wise ass but I'm amazed that code works. I'd suggest some changes:
Code: Select all
mysql_connect("localhost", "root");
mysql_select_db(gbook);
You probably don't need to make the dlid a function. Also, you only need to connect once.
It's not recommended to connect as root user. Create a new user with only the minumum permissions required, and use that in your scripts. Next, make sure the dlid script (above) is in a folder protected by .htaccess (deny from all).
This isn't the finished article but it might get you closer to what you're looking for:
Code: Select all
////////////////////////////// CONFIG ////////////////////////////
// easy to find and change config option, at top of page
$Gb_MsgPerPage = 20;
////////////////////////////////////////////////////////////////////
function Gb_Read($Gb_MsgMode) {
$Gb_MsgPerPage = $GLOBALS['Gb_MsgPerPage'];
$mysql = "SELECT * FROM posts";
$query = mysql_query($mysql) or die("Cannot query the database.<br>" . mysql_error()); // or die bit is always good to have
// use ' if just a string - quicker since doesn't get parsed
echo '<table border=1 ><tr><td>Messages</td></tr>';
$i = 0; // 0 not 1 to get 20 items - either that or "$i <=" in the IF step
while ($gb = mysql_fetch_array($query)) { // just one loop
if ( $i < $Gb_MsgPerPage ) { // use "<" if you are limiting to 20 per page
echo "<tr><td>";
// dot concatenating strings & vars can help avoid problems
echo "<A href=gb.php?GbPage=GbViewPost&id=" . $gb[ID] . "><B>" . $gb[Title] . " </b> </a> ";
echo "<i>Posted by" . $gb[Poster] . " On " . $gb[Date] . "</i>";
echo '</tr></td>'; // ' again
}
$i++;
}
echo '</table>'; // print is OK too I just like to use echo
}
$GbPage = $_GET['GbPage']; // php 4.1.0 on - $HTTP_GET_VARS otherwise
Switch ($GbPage) {
case "GbViewPost":
// your function here
break;
default:
Gb_Read(1);
break;
}
I didn't see where you are using $GbMsgMode in the script. Do you intend to use Gb_Read() for both cases?
If so, you may not need to define it as a function: code is slimmed down by just writing the script (and by doing away with the switch case). Declare $Gb_MsgPerPage & $Gb_MsgMode first if you do that.
QUERY STRING: to help check whether the value you expect is really being passed in the query string you could:
echo $_SERVER['QUERY_STRING'];
.. at the top of the page, or just echo $GBpage after you declare it as a value from the $_GET array.
PS: if you use the BB code php tags and indent it's easier to read your posted code.
Posted: Fri Mar 07, 2003 3:51 pm
by toms100
thank you for taking the time to make that reply.
i am making the functions so that at a later date i can easily use this script as an included script and make use of the functions.
im not sure about the database, i need to get it to work on my remote php server which i will use when i get this working
I see your point about the config at the top, its probally a bit easier
anyway many thanks
Tom
Posted: Fri Mar 07, 2003 3:58 pm
by McGruff
Np. I hope you didn't mind someone picking through the details of your script - trying to be helpful.
If you've got shared hosting, you probably won't need to bother setting up databse permissions ie if you don't have root access to the server.
Posted: Fri Mar 07, 2003 3:59 pm
by josa
To get rid of the error message you could try to substitute the line
Code: Select all
$GbPage = $HTTP_GET_VARSї'GbPage'];
with this line
Code: Select all
$GbPage = (isset($HTTP_GET_VARSї'GbPage'])) ? $HTTP_GET_VARSї'GbPage'] : FALSE;
/josa
Posted: Fri Mar 07, 2003 4:02 pm
by toms100
ive changed my code to the following but i get more errors:
Code: Select all
<?php
////////////////////////////// CONFIG ////////////////////////////
// easy to find and change config option, at top of page
$Gb_MsgPerPage = 20;
////////////////////////////////////////////////////////////////////
function DB() {
$db = mysql_connect("localhost", "root");
return $db;
}
function Gb_Read($Gb_MsgMode) {
$Gb_MsgPerPage = $GLOBALS['Gb_MsgPerPage'];
mysql_select_db("gbook",DB());
$mysql = "SELECT * FROM posts";
$query = mysql_query($mysql,DB()) or die("Cannot query the database.<br>" . mysql_error()); // or die bit is always good to have
// use ' if just a string - quicker since doesn't get parsed
echo '<table border=1 ><tr><td>Messages</td></tr>';
$i = 0; // 0 not 1 to get 20 items - either that or "$i <=" in the IF step
while ($gb = mysql_fetch_array($query)) { // just one loop
if ( $i < $Gb_MsgPerPage ) { // use "<" if you are limiting to 20 per page
echo "<tr><td>";
// dot concatenating strings & vars can help avoid problems
echo "<A href=gb.php?GbPage=GbViewPost&id=" . $gb[ID] . "><B>" . $gb[Title] . " </b> </a> ";
echo "<i>Posted by" . $gb[Poster] . " On " . $gb[Date] . "</i>";
echo '</tr></td>'; // ' again
}
$i++;
}
echo '</table>'; // print is OK too I just like to use echo
}
$GbPage = $_GET['GbPage']; // php 4.1.0 on - $HTTP_GET_VARS otherwise
Switch ($GbPage) {
case "GbViewPost":
// your function here
break;
default:
Gb_Read(1);
break;
}
?>
i still get the one about undefined index gbpage:/
Notice: Undefined index: GbPage in E:\Tom\php work\guestbook\gb.php on line 39
Messages
Notice: Use of undefined constant ID - assumed 'ID' in E:\Tom\php work\guestbook\gb.php on line 30
Notice: Use of undefined constant Title - assumed 'Title' in E:\Tom\php work\guestbook\gb.php on line 30
HEllo
Notice: Use of undefined constant Poster - assumed 'Poster' in E:\Tom\php work\guestbook\gb.php on line 31
Notice: Use of undefined constant Date - assumed 'Date' in E:\Tom\php work\guestbook\gb.php on line 31
Posted byDave On 32
Notice: Use of undefined constant ID - assumed 'ID' in E:\Tom\php work\guestbook\gb.php on line 30
Notice: Use of undefined constant Title - assumed 'Title' in E:\Tom\php work\guestbook\gb.php on line 30
Will it work
Notice: Use of undefined constant Poster - assumed 'Poster' in E:\Tom\php work\guestbook\gb.php on line 31
Notice: Use of undefined constant Date - assumed 'Date' in E:\Tom\php work\guestbook\gb.php on line 31
Posted by On 23
Notice: Use of undefined constant ID - assumed 'ID' in E:\Tom\php work\guestbook\gb.php on line 30
Notice: Use of undefined constant Title - assumed 'Title' in E:\Tom\php work\guestbook\gb.php on line 30
sda
Notice: Use of undefined constant Poster - assumed 'Poster' in E:\Tom\php work\guestbook\gb.php on line 31
Notice: Use of undefined constant Date - assumed 'Date' in E:\Tom\php work\guestbook\gb.php on line 31
Posted by On 5
$GbMsgMode is going to be used after i get this so that the guest book can be displayed either as each post being shown or just the title being shown and you have to click on the post to see the body of the message
Posted: Fri Mar 07, 2003 4:13 pm
by toms100
josa wrote:To get rid of the error message you could try to substitute the line
Code: Select all
$GbPage = $HTTP_GET_VARSї'GbPage'];
with this line
Code: Select all
$GbPage = (isset($HTTP_GET_VARSї'GbPage'])) ? $HTTP_GET_VARSї'GbPage'] : FALSE;
/josa
thanks Josa, that works great on the first page but then when i run through to display the second bit which is show because GbPage=GbViewPost i get
Notice: Use of undefined constant GbViewPost - assumed 'GbViewPost' in E:\Tom\php work\guestbook\gb.php on line 43
Notice: Undefined index: 'id' in E:\Tom\php work\guestbook\gb.php on line 44
View post with ID
hmm not having much luck eh:)
Posted: Fri Mar 07, 2003 4:17 pm
by McGruff
EDITED:
If you use the code I suggested for the dlid (ie not a function and put it in a separate file so you can include as needed) and then take DB() out of the code (select db and the $query line) that might be a step forward.
Of course you might not want to work like that - that's just the only way I know to help.
Posted: Fri Mar 07, 2003 4:25 pm
by McGruff
Or.. I just had a quick look at the manual.
You're using a function DB() where I think you should be using a var.
So, after defining DB(), write this:
$db = DB();
..then replace DB() in your code with $db.
Posted: Fri Mar 07, 2003 4:32 pm
by toms100
sorry but whats a dlid?
i changed the code as you specified but to the same outcome

here is the php
Code: Select all
<?php
////////////////////////////// CONFIG ////////////////////////////
// easy to find and change config option, at top of page
$Gb_MsgPerPage = 20;
////////////////////////////////////////////////////////////////////
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
$start = getmicrotime();
function DB() {
$db = mysql_connect("localhost", "root");
return $db;
}
function Gb_Read($Gb_MsgMode=1) {
$db = DB();
$Gb_MsgPerPage = $GLOBALS['Gb_MsgPerPage'];
mysql_select_db("gbook",$db);
$mysql = "SELECT * FROM posts";
$query = mysql_query($mysql,DB()) or die("Cannot query the database.<br>" . mysql_error()); // or die bit is always good to have
// use ' if just a string - quicker since doesn't get parsed
echo '<table border=1 ><tr><td>Messages</td></tr>';
$i = 0; // 0 not 1 to get 20 items - either that or "$i <=" in the IF step
while ($gb = mysql_fetch_array($query)) { // just one loop
if ( $i < $Gb_MsgPerPage ) { // use "<" if you are limiting to 20 per page
echo "<tr><td>";
// dot concatenating strings & vars can help avoid problems
echo "<A href=gb.php?GbPage=GbViewPost&id=" . $gb[ID] . "><B>" . $gb[Title] . " </b> </a> ";
echo "<i>Posted by" . $gb[Poster] . " On " . $gb[Date] . "</i>";
echo '</tr></td>'; // ' again
}
$i++;
}
echo '</table>'; // print is OK too I just like to use echo
}
$GbPage = (isset($HTTP_GET_VARS['GbPage'])) ? $HTTP_GET_VARS['GbPage'] : FALSE;
if ($GbPage) {
Switch ($GbPage) {
// Displays info based on the querystring result. This is the meat of the script
case GbViewPost:
echo "View post with ID $HTTP_GET_VARS['id'] ";
break;
}
}
else {
Gb_Read();
$end = getmicrotime();
$time = $end - $start;
echo "<p>time=" . $time . "</p>";
}
?>
?>
the timer works great.
i presume the outputed time such as 0.011677980423 is seconds?
Posted: Fri Mar 07, 2003 4:39 pm
by josa
Three things:
1. You should always have quotes around keys when using arrays, like this:
Code: Select all
echo $gbї'Title']; //with single quotes...
echo $gbї"Title"] //...or double quotes
echo $gbїTitle] //this will give a notice
2. Keys are case sensitive:
Code: Select all
$gbї'ID'] = 'Just testing...';
echo $gbї'ID']; //this prints: Just testing...
echo $gbї'Id']; //this will give a notice
3. You have to test if a key/value pair exist before you try to access it:
Code: Select all
$GbPage = $_GETї'GbPage']; //will give a notice if there's no value set for GbPage in the URL.
/josa
Posted: Fri Mar 07, 2003 4:41 pm
by McGruff
dlid is a database link identifier - the DB() function
rgr time in secs
There's still a DB() in the mysql_query() - try replacing with $db. I don't know if you can put a function in there like that.
Is the switch case needed?
Posted: Sat Mar 08, 2003 1:17 am
by McGruff
I added some stuff which might help you debug:
Code: Select all
////////////////////////////// CONFIG ////////////////////////////
// easy to find and change config option, at top of page
$Gb_MsgPerPage = 20;
////////////////////////////////////////////////////////////////////
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
$start = getmicrotime();
function DB() {
$db = mysql_connect("localhost", "root");
return $db;
}
function Gb_Read($Gb_MsgMode=1) {
$db = DB();
IF (is_resource($db)) {
echo 'db resource exists<br>';
} ELSE {
echo 'db resource doesn''t exist<br>';
}
$Gb_MsgPerPage = $GLOBALS['Gb_MsgPerPage'];
mysql_select_db("gbook",$db);
$mysql = "SELECT * FROM posts";
$query = mysql_query($mysql, $db) or die("Cannot query the database.<br>" . mysql_error()); // or die bit is always good to have
IF (is_resource($query)) {
echo 'query resource exists<br>';
} ELSE {
echo 'query resource doesn''t exist<br>';
}
echo '<table border=1 ><tr><td>Messages</td></tr>';
$i = 0; // 0 not 1 to get 20 items - either that or "$i <=" in the IF step
while ($gb = mysql_fetch_array($query)) { // just one loop
if ( $i < $Gb_MsgPerPage ) { // use "<" if you are limiting to 20 per page
echo "<tr><td>";
print_r($gb);
echo '<br>';
// dot concatenating strings & vars can help avoid problems
echo "<A href=gb.php?GbPage=GbViewPost&id=" . $gb[ID] . "><B>" . $gb[Title] . " </b> </a> ";
echo "<i>Posted by" . $gb[Poster] . " On " . $gb[Date] . "</i>";
echo '</tr></td>'; // ' again
}
$i++;
}
echo '</table>'; // print is OK too I just like to use echo
}
$GbPage = (isset($HTTP_GET_VARS['GbPage'])) ? $HTTP_GET_VARS['GbPage'] : FALSE;
echo "GbPage=" . $GbPage . "<br>";
if ($GbPage) {
Switch ($GbPage) {
// Displays info based on the querystring result. This is the meat of the script
case GbViewPost:
echo "View post with ID $HTTP_GET_VARS['id'] ";
break;
}
}
else {
Gb_Read();
$end = getmicrotime();
$time = $end - $start;
echo "<p>time=" . $time . "</p>";
}
Posted: Sat Mar 08, 2003 3:02 am
by toms100
woooohooo i got it working!
in the $gb[databaseentry] i added 'databaseentry' (quotes) and now its error free!
i really appreciate the help you lot have given especially McGruff:)