Jump to content

Notice: Undefined variable: row in....


wix100

Recommended Posts

Hello all - am looking for help with dynamic links in PHP not returning data and coughing out "Notice: Undefined variable: row in /home/jj3wg2td/public_html/Uni More Info.php on line 200"

The initial search is being fed from a mysql into table of results in another page ('ViewMore' being a column in my database and 'UniversityCourses' being the trigger to search that column):

echo "<td class='result'><a href='Uni More Info.php?ID={$row['UniversityCourses']}'>{$row['ViewMore']}</a></td>";

This is working fine as I can see the various links changing when you hover over them in the table.

clicking the links goes to "Uni More Info.php"

where my PHP code is:

<?php
session_start();
	
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);	

//Connect to the database
$mysqli = new mysqli("localhost", "jj3wg2td_wix", "MYPASSWORD", "MYDATABASE");

if(isset($_GET['post'])){

	$ID=mysqli_real_escape_string($mysqli, $_GET['ID']);
	
	$sql="SELECT * FROM uni2020 WHERE UniversityCourses='$ID'";
	$result=mysqli_query($mysqli,$sql) or die ("Bad Query: $sql");
	$row=mysqli_fetch_array($result);
	
	if(isset($_GET['post'])){
		$Website=mysqli_real_escape_string($mysqli,$_GET['Website']);
		$UcasPointsRequired=mysqli_real_escape_string($mysqli,$_GET['UcasPointsRequired']);
		$OverallScore=mysqli_real_escape_string($mysqli,$_GET['OverallScore']);
		
	$sql= "INSERT INTO UniversityCourses (Website, UcasPointsRequired) VALUES ('$Website', $UcasPointsRequired');";
	$result=mysqli_query($mysqli,$sql) or die ("Bad Query: $sql");
	}
}	
	
?>
<content>
	<?php echo $row['Website']?>
	<?php echo $row['UcasPointsRequired']?>
	<?php echo $row['OverallScore']?>
</content>

The error codes are showing:

Notice: Undefined variable: row in /home/jj3wg2td/public_html/Uni More Info.php on line 200

Notice: Undefined variable: row in /home/jj3wg2td/public_html/Uni More Info.php on line 201

Notice: Undefined variable: row in /home/jj3wg2td/public_html/Uni More Info.php on line 202

Which is the section in-between the <content></content> tags.

Reading up into "Undefined Variable", it suggest that I havent  "Declared the variables", but isn't that this section?:

if(isset($_GET['post'])){
		$Website=mysqli_real_escape_string($mysqli,$_GET['Website']);
		$UcasPointsRequired=mysqli_real_escape_string($mysqli,$_GET['UcasPointsRequired']);
		$OverallScore=mysqli_real_escape_string($mysqli,$_GET['OverallScore']);

I am new to PHP and am learning as I go along....but yet again...I'm stumped!

Any help is very much appreciated (again)!!!!

 

Steven

Link to comment
Share on other sites

Steven,  See below.  Also, you probably want to make a HTTP POST request instead of GET.

if(isset($_GET['post'])){
   // bla bla bla
   $row=mysqli_fetch_array($result);
}    
    
?>
<!-- What if $_GET['post'] wasn't set? Will $row be defined? -->
<content>
    <?php echo $row['Website']?>
    <?php echo $row['UcasPointsRequired']?>
    <?php echo $row['OverallScore']?>
</content>

 

Link to comment
Share on other sites

27 minutes ago, NotionCommotion said:

Steven,  See below.  Also, you probably want to make a HTTP POST request instead of GET.


if(isset($_GET['post'])){
   // bla bla bla
   $row=mysqli_fetch_array($result);
}    
    
?>
<!-- What if $_GET['post'] wasn't set? Will $row be defined? -->
<content>
    <?php echo $row['Website']?>
    <?php echo $row['UcasPointsRequired']?>
    <?php echo $row['OverallScore']?>
</content>

 

Hi NotionCommotion - thanks for getting back at lightning speed!!

There's a clue in there somewhere! :)

I thought you might have been pointing to the order that I had put the code....thinking that the $mysqli connection string needed to be lower, so that it connected to the database once the $_GET code was evident?

if(isset($_GET['post'])){
	//Connect to the database
	$mysqli = new mysqli("localhost", "BLAHBLAH", "BLAHBLAH", "BLAHBLAH");

	$ID=mysqli_real_escape_string($mysqli, $_GET['ID']);

//BLAHBLAHBLAHBLAH
	

I know I'm missing something very simple!?

Link to comment
Share on other sites

11 hours ago, Barand said:

Yes, you are - the answer was given to you ...

 

The answer to the above question, BTW, is NO.

Hi both, and thanks again!

I can only apologise for my ignorance (stupidity), but am reading that I have missed the:

$row=mysqli_fetch_array($result);
}    

at the end, to fetch the data?

so PHP code now being:

<?php
session_start();
	
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);	

//Connect to the database
$mysqli = new mysqli("localhost", "BLAHBLAH", "BLAHBLAH", "BLAHBLAH");

if(isset($_GET['post'])){

	$ID=mysqli_real_escape_string($mysqli, $_GET['ID']);
	
	$sql="SELECT * FROM uni2020 WHERE UniversityCourses='$ID'";
	$result=mysqli_query($mysqli,$sql) or die ("Bad Query: $sql");
	$row=mysqli_fetch_array($result);
	
	if(isset($_GET['post'])){
		$Website=mysqli_real_escape_string($mysqli,$_GET['Website']);
		$UcasPointsRequired=mysqli_real_escape_string($mysqli,$_GET['UcasPointsRequired']);
		$OverallScore=mysqli_real_escape_string($mysqli,$_GET['OverallScore']);
		
	$sql= "INSERT INTO UniversityCourses (Website, UcasPointsRequired) VALUES ('$Website', $UcasPointsRequired');";
	$result=mysqli_query($mysqli,$sql) or die ("Bad Query: $sql");
	}
	$row=mysqli_fetch_array($result);
}	
	
?>
<content>
	<?php echo $row['Website']?>
	<?php echo $row['UcasPointsRequired']?>
	<?php echo $row['OverallScore']?>
</content>

but this brings back the same error of:

"Notice: Undefined variable: row in"

Am sure I am over complicating all of this....but I am trying, I promise!

Thanks again!

Link to comment
Share on other sites

3 hours ago, wix100 said:

Hi both, and thanks again!

I can only apologise for my ignorance (stupidity), but am reading that I have missed the:


$row=mysqli_fetch_array($result);
}    

at the end, to fetch the data?

so PHP code now being:


<?php
session_start();
	
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);	

//Connect to the database
$mysqli = new mysqli("localhost", "BLAHBLAH", "BLAHBLAH", "BLAHBLAH");

if(isset($_GET['post'])){

	$ID=mysqli_real_escape_string($mysqli, $_GET['ID']);
	
	$sql="SELECT * FROM uni2020 WHERE UniversityCourses='$ID'";
	$result=mysqli_query($mysqli,$sql) or die ("Bad Query: $sql");
	$row=mysqli_fetch_array($result);
	
	if(isset($_GET['post'])){
		$Website=mysqli_real_escape_string($mysqli,$_GET['Website']);
		$UcasPointsRequired=mysqli_real_escape_string($mysqli,$_GET['UcasPointsRequired']);
		$OverallScore=mysqli_real_escape_string($mysqli,$_GET['OverallScore']);
		
	$sql= "INSERT INTO UniversityCourses (Website, UcasPointsRequired) VALUES ('$Website', $UcasPointsRequired');";
	$result=mysqli_query($mysqli,$sql) or die ("Bad Query: $sql");
	}
	$row=mysqli_fetch_array($result);
}	
	
?>
<content>
	<?php echo $row['Website']?>
	<?php echo $row['UcasPointsRequired']?>
	<?php echo $row['OverallScore']?>
</content>

but this brings back the same error of:

"Notice: Undefined variable: row in"

Am sure I am over complicating all of this....but I am trying, I promise!

Thanks again!

Hi Guys - me again (sorry)

So, now have the code of:

<?php
session_start();
	
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);	

if(isset($_GET['ID'])){
	
//Connect to the database
$mysqli = new mysqli("localhost", "jj3wg2td_wix", "BLAH BLAH", "BLAH BLAH");

	
	$ID=mysqli_real_escape_string($mysqli, $_GET['ID']);
	
	$sql="SELECT * FROM uni2020 WHERE UniversityCourses='$ID'";    
	
	$result=mysqli_query($mysqli,$sql) or die ("Bad Query: $sql");
	if($result->num_rows > 0);
	
	
	
	if(isset($_GET['ID'])){
		$Website=mysqli_real_escape_string($mysqli,$_GET['Website']);
		$UcasPointsRequired=mysqli_real_escape_string($mysqli,$_GET['UcasPointsRequired']);
		$OverallScore=mysqli_real_escape_string($mysqli,$_GET['OverallScore']);
	}
	$row=mysqli_fetch_array($result);
	
	}else{
		header('Location: Uni Results.php');
}
?>
<section>
	<?php echo $row['Website']?>
	<?php echo $row['UcasPointsRequired']?>
	<?php echo $row['OverallScore']?>
</section>

I was missing the:

if($result->num_rows > 0);

Correct?

This now returns the results (thanks) but am now getting the error:

"Notice: Undefined index" on each OUTPUT ECHO line...

Link to comment
Share on other sites

if you are currently getting undefined index errors, it means $row exists, but doesn't contain what you think. what columns are in the uni2020 table? what does using var_dump($row); show?

5 hours ago, wix100 said:

if($result->num_rows > 0);

the above line of code is not doing anything, because of the ; on the end of it.

a lot of these issues would not exist if you organized your code better. your code should be laid out in this general order -

  1. initialization - define, require, create, ... things your page needs, such as the session_start() statement, a database connection, configuration values, ...
  2.  post method form processing code - a post method form should be used for things that create/update data on the server or perform an action such as sending an email. you should detect if a post method form has been submitted before using any of the form data.
  3. get method business logic - get/create data needed to display the dynamic content on the web page. you should fetch the data from any query into an appropriately named php variable, then test/reference that variable in the html document.
  4. html document/template - using simple php code or an actual template system, produce the actual html document, using the data produced from the above sections of code.

the php error settings should be in the php.ini on your system, not in your code file.

next, you need to validate all inputs to any section of code before using them, setting up and displaying a user error message for any 'required' input that doesn't contain an expected value. if $_GET['ID'] is required for the page to work, it is a user error if it doesn't exist. you should be setting up a message for the user telling them that no UniversityCourse has been selected. at the point of echoing the result from the SELECT query, if the query did not match any data, you should be outputting a message stating so, and only attempt to echo the values if they exist. inside the post method form processing code, if any of the inputs are 'required', but they are empty, that's a user error. you should be setting up a unique message for each empty input, telling the user which inputs they did not enter a value for.

Edited by mac_gyver
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.