Jump to content
Larry Ullman's Book Forums

Undefined Variable On Browse.Php Script


Recommended Posts

Hi everyone,

 

I'm having trouble figuring out this error on my own. On page 210, I'm trying to make a similar browse.php script. I made the stored procedure like in the book except I don't have a category section. When I run the script I keep getting a "Undefined variable". Here is the code.

 

<?php #Browse_fudge.php

require('includes/config.php');

//This page displays the available Fudge

//Set the config file:

 

 

if(isset($_GET['id'])&&

is_numeric($_GET['id']) ){

 

$fid = (int) $_GET['id'];

}

 

//Require the database connection

require_once('../mysqli_connect.php');

 

//Set the page title and include the HTML header:

$page_title='Fudge';

include('includes/header.html');

 

 

//Get fudge's query:

$r=mysqli_query($dbc,"CALL select_fudge('$fid')");

if(mysqli_num_rows($r)>=1)

{

include('./views/list_fudge.html');

 

}else{

include('./views/noproducts.html');

}

 

include('includes/footer.html');

 

?>

 

 

------Here is the error I get.----------

 

An error occurred in script 'C:\xampp\htdocs\htdocs\fudge\browse_fudge.php' on line 22:

Undefined variable: fid

 

Array

 

(

 

[0] => Array

 

(

 

[file] => C:\xampp\htdocs\htdocs\fudge\browse_fudge.php

 

[line] => 22

 

[function] => my_error_handler

 

[args] => Array

 

(

 

[0] => 8

 

[1] => Undefined variable: fid

 

[2] => C:\xampp\htdocs\htdocs\fudge\browse_fudge.php

 

[3] => 22

 

[4] => Array

 

(

 

[GLOBALS] => Array

 

*RECURSION*

 

[_POST] => Array

 

(

 

)

 

 

 

[_GET] => Array

 

(

 

)

 

 

 

[_COOKIE] => Array

 

(

 

)

 

 

 

[_FILES] => Array

 

(

 

)

 

 

 

[live] =>

 

[contact_email] => ********

 

[dbc] => mysqli Object

 

(

 

[affected_rows] => 0

 

[client_info] => mysqlnd 5.0.7-dev - 091210 - $Revision: 304625 $

 

[client_version] => 50007

 

[connect_errno] => 0

 

[connect_error] =>

 

[errno] => 0

 

[error] =>

 

[field_count] => 0

 

[host_info] => localhost via TCP/IP

 

[info] =>

 

[insert_id] => 0

 

[server_info] => 5.5.8

 

[server_version] => 50508

 

[sqlstate] => 00000

 

[protocol_version] => 10

 

[thread_id] => 580

 

[warning_count] => 0

 

)

 

 

 

[page_title] => Fudge

 

)

 

 

 

)

 

 

 

)

 

 

 

)

 

 

An error occurred in script 'C:\xampp\htdocs\htdocs\fudge\browse_fudge.php' on line 28:

include(./views/noproducts.html) [function.include]: failed to open stream: No such file or directory

 

Array

 

(

 

[0] => Array

 

(

 

[file] => C:\xampp\htdocs\htdocs\fudge\browse_fudge.php

 

[line] => 28

 

[function] => my_error_handler

 

[args] => Array

 

(

 

[0] => 2

 

[1] => include(./views/noproducts.html) [function.include]: failed to open stream: No such file or directory

 

[2] => C:\xampp\htdocs\htdocs\fudge\browse_fudge.php

 

[3] => 28

 

[4] => Array

 

(

 

[GLOBALS] => Array

 

*RECURSION*

 

[_POST] => Array

 

(

 

)

 

 

 

[_GET] => Array

 

(

 

)

 

 

 

[_COOKIE] => Array

 

(

 

)

 

 

 

[_FILES] => Array

 

(

 

)

 

 

 

[live] =>

 

[contact_email] => janicepag23@yahoo.com

 

[dbc] => mysqli Object

 

(

 

[affected_rows] => 0

 

[client_info] => mysqlnd 5.0.7-dev - 091210 - $Revision: 304625 $

 

[client_version] => 50007

 

[connect_errno] => 0

 

[connect_error] =>

 

[errno] => 0

 

[error] =>

 

[field_count] => 6

 

[host_info] => localhost via TCP/IP

 

[info] =>

 

[insert_id] => 0

 

[server_info] => 5.5.8

 

[server_version] => 50508

 

[sqlstate] => 00000

 

[protocol_version] => 10

 

[thread_id] => 580

 

[warning_count] => 0

 

)

 

 

 

[page_title] => Fudge

 

[r] => mysqli_result Object

 

(

 

[current_field] => 0

 

[field_count] => 6

 

[lengths] =>

 

[num_rows] => 0

 

[type] => 0

 

)

 

 

 

)

 

 

 

)

 

 

 

)

 

 

 

[1] => Array

 

(

 

[file] => C:\xampp\htdocs\htdocs\fudge\browse_fudge.php

 

[line] => 28

 

[function] => include

 

)

 

 

 

)

 

 

An error occurred in script 'C:\xampp\htdocs\htdocs\fudge\browse_fudge.php' on line 28:

include() [function.include]: Failed opening './views/noproducts.html' for inclusion (include_path='.;C:\xampp\php\PEAR')

 

Array

 

(

 

[0] => Array

 

(

 

[file] => C:\xampp\htdocs\htdocs\fudge\browse_fudge.php

 

[line] => 28

 

[function] => my_error_handler

 

[args] => Array

 

(

 

[0] => 2

 

[1] => include() [function.include]: Failed opening './views/noproducts.html' for inclusion (include_path='.;C:\xampp\php\PEAR')

 

[2] => C:\xampp\htdocs\htdocs\fudge\browse_fudge.php

 

[3] => 28

 

[4] => Array

 

(

 

[GLOBALS] => Array

 

*RECURSION*

 

[_POST] => Array

 

(

 

)

 

 

 

[_GET] => Array

 

(

 

)

 

 

 

[_COOKIE] => Array

 

(

 

)

 

 

 

[_FILES] => Array

 

(

 

)

 

 

 

[live] =>

 

[contact_email] => *********

 

[dbc] => mysqli Object

 

(

 

[affected_rows] => 0

 

[client_info] => mysqlnd 5.0.7-dev - 091210 - $Revision: 304625 $

 

[client_version] => 50007

 

[connect_errno] => 0

 

[connect_error] =>

 

[errno] => 0

 

[error] =>

 

[field_count] => 6

 

[host_info] => localhost via TCP/IP

 

[info] =>

 

[insert_id] => 0

 

[server_info] => 5.5.8

 

[server_version] => 50508

 

[sqlstate] => 00000

 

[protocol_version] => 10

 

[thread_id] => 580

 

[warning_count] => 0

 

)

 

 

 

[page_title] => Fudge

 

[r] => mysqli_result Object

 

(

 

[current_field] => 0

 

[field_count] => 6

 

[lengths] =>

 

[num_rows] => 0

 

[type] => 0

 

)

 

 

 

)

 

 

 

)

 

 

 

)

 

 

 

[1] => Array

 

(

 

[file] => C:\xampp\htdocs\htdocs\fudge\browse_fudge.php

 

[line] => 28

 

[function] => include

 

)

 

 

 

)

 

 

Thanks

Janice

Link to comment
Share on other sites

It looks as if $fid only will be defined if the $_GET['id'] statement is true, so that is where I would start looking.

 

You could add a temporary line such as echo $fid; to check what $fid has been set to,

and even set $fid = "1234" (or whatever) to check if the program works when $fid is defined corrrectly.

 

 

In the errors list I note that the file /views/noproduct.html cannot be found or included. Might be worth checking this file is on the correct location.

Link to comment
Share on other sites

First, I would consistently use either fid or id. Right now you assume $_GET['id'] but assign this to $fid. Second, you have code that only creates $fid if $_GET['id'] is set and numeric, which is good, but your code later in the script attempts to use $fid in a query whether or not it exists, which is bad.

Link to comment
Share on other sites

Well I made a small change just to see if it works. I do get that one item but when i use $_GET['id'] it doesn't work and can't find out why.

 

----I changed----

 

if(isset($_GET['id'])&&

is_numeric($_GET['id']) ){

 

$fid = (int) $_GET['id'];

}

 

----to----

 

$id = 2;

Link to comment
Share on other sites

I'm in agreement with Larry and chris14 that the source of the error has to be coming from the fact that $fid is not being set. As such, you need to look into that. Either $_GET['id'] is not set or it's not a number, for one reason or another.

 

Hope you find the problem soon.

Link to comment
Share on other sites

 Share

×
×
  • Create New...