Querystring query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Querystring query

Post 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() &#123;
$db = mysql_connect("localhost", "root");
return $db;
&#125;

function Gb_Read($Gb_MsgPerPage=20,$Gb_MsgMode=1) &#123;
// 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))  &#123;

do &#123;
if ( $i > $Gb_MsgPerPage ) &#123;
break; &#125;
print 
print "<tr><td>";
print ("<A href=gb.php?GbPage=GbViewPost&id=$gb&#1111;ID]><B> $gb&#1111;Title] </b> </a> ");
print ("<i>Posted by $gb&#1111;Poster]");
print ("On $gb&#1111;Date]</i>");
print ("</tr></td>");
$i++;
&#125; while ($gb = mysql_fetch_array($result));
&#125;
print ("</table>");
&#125;
$GbPage = $HTTP_GET_VARS&#1111;'GbPage'];

if ($GbPage) &#123;
Switch ($GbPage) &#123;
// Displays info based on the querystring result. This is the meat of the script
case GbViewPost:
echo "View post with ID";
break;
&#125;
&#125;
else &#123;
Gb_Read();
&#125;

?>
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
?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
Last edited by McGruff on Thu Aug 11, 2005 1:55 pm, edited 3 times in total.
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post 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
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
Last edited by McGruff on Fri Mar 07, 2003 4:01 pm, edited 1 time in total.
josa
Forum Commoner
Posts: 75
Joined: Mon Jun 24, 2002 4:58 am
Location: Sweden

Post by josa »

To get rid of the error message you could try to substitute the line

Code: Select all

$GbPage = $HTTP_GET_VARS&#1111;'GbPage'];
with this line

Code: Select all

$GbPage = (isset($HTTP_GET_VARS&#1111;'GbPage'])) ? $HTTP_GET_VARS&#1111;'GbPage'] : FALSE;
/josa
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post 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
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post 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&#1111;'GbPage'];
with this line

Code: Select all

$GbPage = (isset($HTTP_GET_VARS&#1111;'GbPage'])) ? $HTTP_GET_VARS&#1111;'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:)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post 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?
josa
Forum Commoner
Posts: 75
Joined: Mon Jun 24, 2002 4:58 am
Location: Sweden

Post by josa »

Three things:

1. You should always have quotes around keys when using arrays, like this:

Code: Select all

echo $gb&#1111;'Title']; //with single quotes...
echo $gb&#1111;"Title"] //...or double quotes
echo $gb&#1111;Title] //this will give a notice
2. Keys are case sensitive:

Code: Select all

$gb&#1111;'ID'] = 'Just testing...';
echo $gb&#1111;'ID']; //this prints: Just testing...
echo $gb&#1111;'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&#1111;'GbPage']; //will give a notice if there's no value set for GbPage in the URL.
/josa
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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>"; 

}
Last edited by McGruff on Thu Aug 11, 2005 1:53 pm, edited 1 time in total.
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post 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:)
Post Reply