wix100 Posted August 24, 2020 Share Posted August 24, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/311378-notice-undefined-variable-row-in/ Share on other sites More sharing options...
NotionCommotion Posted August 24, 2020 Share Posted August 24, 2020 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> Quote Link to comment https://forums.phpfreaks.com/topic/311378-notice-undefined-variable-row-in/#findComment-1580926 Share on other sites More sharing options...
wix100 Posted August 24, 2020 Author Share Posted August 24, 2020 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!? Quote Link to comment https://forums.phpfreaks.com/topic/311378-notice-undefined-variable-row-in/#findComment-1580927 Share on other sites More sharing options...
Barand Posted August 24, 2020 Share Posted August 24, 2020 2 hours ago, wix100 said: I know I'm missing something very simple!? Yes, you are - the answer was given to you ... 3 hours ago, NotionCommotion said: <!-- What if $_GET['post'] wasn't set? Will $row be defined? --> The answer to the above question, BTW, is NO. Quote Link to comment https://forums.phpfreaks.com/topic/311378-notice-undefined-variable-row-in/#findComment-1580930 Share on other sites More sharing options...
wix100 Posted August 25, 2020 Author Share Posted August 25, 2020 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! Quote Link to comment https://forums.phpfreaks.com/topic/311378-notice-undefined-variable-row-in/#findComment-1580949 Share on other sites More sharing options...
wix100 Posted August 25, 2020 Author Share Posted August 25, 2020 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... Quote Link to comment https://forums.phpfreaks.com/topic/311378-notice-undefined-variable-row-in/#findComment-1580951 Share on other sites More sharing options...
Barand Posted August 25, 2020 Share Posted August 25, 2020 You have this situation if (....) { // define row row = something } echo row; // here you attempt to use row whether it was defined or not Quote Link to comment https://forums.phpfreaks.com/topic/311378-notice-undefined-variable-row-in/#findComment-1580953 Share on other sites More sharing options...
mac_gyver Posted August 25, 2020 Share Posted August 25, 2020 (edited) 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 - initialization - define, require, create, ... things your page needs, such as the session_start() statement, a database connection, configuration values, ... 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. 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. 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 August 25, 2020 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/311378-notice-undefined-variable-row-in/#findComment-1580962 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.