Click here to Skip to main content
14,775,260 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
am following a tutorial and am creating a skeleton project. I have encountered this:

Notice: Trying to access array offset on value of type null in C:messages.php on line 33

Notice: Trying to access array offset on value of type null in C:messages.php on line 34


I do not know how to fix this :( anyone can help? - here is my page so far!

  1  !DOCTYPE html
  2  <?php
  3  session_start();
  4  include("includes/header.php");
  5  
  6  if(!isset($_SESSION['user_email'])){
  7  	header("location: index.php");
  8  }
  9  ?>
 10  <html>
 11  <head>
 12  	<title>Messages</title>
 13  	<meta charset="utf-8">
 14   	<meta name="viewport" content="width=device-width, initial-scale=1">
 15  	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
 16  	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 17  	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
 18  	<link rel="stylesheet" type="text/css" href="style/home_style2.css">
 19  </head>
 20  <body>
 21  	<div class="row">
 22      	<?php
 23  			if(isset($_GET['u_id'])){
 24  				global $con;
 25  				
 26  				$get_id = $_GET['u_id'];
 27  				
 28  				$get_user = "select * from users where user_id='$get_id'";
 29  				
 30  				$run_user = mysqli_query($con, $get_user);
 31  				$row_user = mysqli_fetch_array($run_user);
 32  				
 33  				$user_to_msg = $row_user['user_id'];
 34  				$user_to_name = $row_user['user_name'];
 35  			}
 36  			
 37  			$user = $_SESSION['user_email'];
 38  			$get_user = "select * from users where user_email='$user'";
 39  			$run_user = mysqli_query($con, $get_user);
 40  			$row = mysqli_fetch_array($run_user);
 41  			
 42  			$user_from_msg = $row['user_id'];
 43  			$user_from_name = $row['user_name'];
 44  		
 45  		?>
 46          <div class="col-sm-3" id="select_user">
 47          <?php 
 48  			$user = "select * from users";
 49  			
 50  			$run_user = mysqli_query($con, $user);
 51  			while($row_user = mysqli_fetch_array($run_user)){
 52  				$user_id = $row_user['user_id'];
 53  				$user_name = $row_user['user_name'];
 54  				$first_name = $row_user['f_name'];
 55  				$last_name = $row_user['l_name'];
 56  				$user_image = $row_user['user_image'];
 57  				
 58  				echo"
 59  					<div class='container-fluid'>
 60  						<a style='text-decoration: none; cursor: ponter ; color: #3897F0;'href='messages.php?u_id=$user_id'>
 61  						<img class='img-circle' src='users/$user_image' width='90px' height='80px' title='$user_name'>  $first_name $last_name<br><br>
 62  						</a>
 63  					</div>
 64  				";
 65  			}
 66  		?>
 67          </div>
 68           <div class="col- sm-6">
 69           	<div class="load_msg" id="scroll_messages">
 70              	<?php
 71  					$sel_msg = "select * from user_messages where (user_to='$user_to_msg' AND user_from='$user_from_msg') OR (user_from='user_to_msg' AND user_to='$user_from_msg') ORDER by 1 ASC";
 72  					$run_msg = mysqli_query($con, $sel_msg);
 73  					
 74  					while($row_msg = mysqli_fetch_array($run_msg)){
 75  						
 76  						$user_to = $row_msg['user_to'];
 77  						$user_from = $row_msg['user_from'];
 78  						$user_body = $row_msg['msg_body'];
 79  						$msg_date = $row_msg['date'];
 80  						?>
 81                          
 82                          <div id="loaded_msg">
 83                          <p><?php if($user_to == $user_to_msg AND $user_from == $user_from_msg){echo "<div class='message' id='blue' data-toggle='tooltip' title='$msg_date'>$msg_body</div><br><br><br>";}else if($user_from == $user_to_msg AND $user_to ==$user_from_msg){echo" <div class='message' id='green' data-toggle='tooltip' title='$msg_date'>$msg_body</div><br><br><br> ";}?></p>
 84                         </div>
 85                         <?php
 86                          
 87  					}
 88  				
 89  				?>
 90              </div>
 91           </div>
 92  	</div>
 93  	</body>
 94  	</html>


What I have tried:

I have tried adding
(int) $_GET['u_id']  and then changing the if statement to <pre>if(isset($_GET['u_id']) && (int)$_GET['u_id'])


which got rid of the errors on line 33 and 34 - but gave me an error here on line 72:
Notice: Undefined variable: user_to_msg in C:messages.php on line 72


so here is where my page is at now - can someone help me solve this?

  1  <div class="row">
  2      	<?php
  3  			if(isset($_GET['u_id']) && (int)$_GET['u_id']) {
  4  				global $con;
  5  				
  6  				(int) $_GET['u_id'];
  7  				$get_id = $_GET['u_id'];
  8  				
  9  				$get_user = "select * from users where user_id='$get_id'";
 10  				
 11  				$run_user = mysqli_query($con, $get_user);
 12  				$row_user = mysqli_fetch_array($run_user);
 13  				
 14  				$user_to_msg = $row_user['user_id'];
 15  				$user_to_name = $row_user['user_name'];
 16  			}
 17  			
 18  			$user = $_SESSION['user_email'];
 19  			$get_user = "select * from users where user_email='$user'";
 20  			$run_user = mysqli_query($con, $get_user);
 21  			$row = mysqli_fetch_array($run_user);
 22  			
 23  			$user_from_msg = $row['user_id'];
 24  			$user_from_name = $row['user_name'];
 25  		
 26  		?>
 27          <div class="col-sm-3" id="select_user">
 28          <?php 
 29  			$user = "select * from users";
 30  			
 31  			$run_user = mysqli_query($con, $user);
 32  			while($row_user = mysqli_fetch_array($run_user)){
 33  				$user_id = $row_user['user_id'];
 34  				$user_name = $row_user['user_name'];
 35  				$first_name = $row_user['f_name'];
 36  				$last_name = $row_user['l_name'];
 37  				$user_image = $row_user['user_image'];
 38  				
 39  				echo"
 40  					<div class='container-fluid'>
 41  						<a style='text-decoration: none; cursor: ponter ; color: #3897F0;'href='messages.php?u_id=$user_id'>
 42  						<img class='img-circle' src='users/$user_image' width='90px' height='80px' title='$user_name'>  $first_name $last_name<br><br>
 43  						</a>
 44  					</div>
 45  				";
 46  			}
 47  		?>
 48          </div>
 49           <div class="col- sm-6">
 50           	<div class="load_msg" id="scroll_messages">
 51              	<?php
 52  					$sel_msg = "select * from user_messages where (user_to='$user_to_msg' AND user_from='$user_from_msg') OR (user_from='user_to_msg' AND user_to='$user_from_msg') ORDER by 1 ASC";
 53  					$run_msg = mysqli_query($con, $sel_msg);
 54  					
 55  					while($row_msg = mysqli_fetch_array($run_msg)){
 56  						
 57  						$user_to = $row_msg['user_to'];
 58  						$user_from = $row_msg['user_from'];
 59  						$user_body = $row_msg['msg_body'];
 60  						$msg_date = $row_msg['date'];
 61  						?>
 62                          
 63                          <div id="loaded_msg">
 64                          <p><?php if($user_to == $user_to_msg AND $user_from == $user_from_msg){echo "<div class='message' id='blue' data-toggle='tooltip' title='$msg_date'>$msg_body</div><br><br><br>";}else if($user_from == $user_to_msg AND $user_to ==$user_from_msg){echo" <div class='message' id='green' data-toggle='tooltip' title='$msg_date'>$msg_body</div><br><br><br> ";}?></p>
 65                         </div>
 66                         <?php
 67                          
 68  					}
 69  				
 70  				?>
 71              </div>
 72           </div>
 73  	</div>
 74  	</body>
 75  	</html>
Posted
Updated 21-Jan-21 3:34am
v2
Comments
Richard MacCutchan 21-Jan-21 8:38am
   
The variable user_to_msg is set inside the block starting at line 3, so it does not exist when the code gets past that block. You need to declare it outside the if block so it is available in the scope of the later code.
jennannegraham 21-Jan-21 14:00pm
   
yes! this makes sense! but I do not know how to do that, I am unsure of how to put it into this specific code block..*sigh*

 <div class="col- sm-6">
         	<div class="load_msg" id="scroll_messages">
            	<?php
					$sel_msg = "select * from user_messages where (user_to='$user_to_msg' AND user_from='$user_from_msg') OR (user_from='user_to_msg' AND user_to='$user_from_msg') ORDER by 1 ASC";
					$run_msg = mysqli_query($con, $sel_msg);
					
					while($row_msg = mysqli_fetch_array($run_msg)){
						
						$user_to = $row_msg['user_to'];
						$user_from = $row_msg['user_from'];
						$user_body = $row_msg['msg_body'];
						$msg_date = $row_msg['date'];
						?>
                        
                        <div id="loaded_msg">
                        <p><?php if($user_to == $user_to_msg AND $user_from == $user_from_msg){echo "<div class='message' id='blue' data-toggle='tooltip' title='$msg_date'>$msg_body</div><br><br><br>";}else if($user_from == $user_to_msg AND $user_to ==$user_from_msg){echo" <div class='message' id='green' data-toggle='tooltip' title='$msg_date'>$msg_body</div><br><br><br> ";}?></p>
                       </div>
                       <?php
                        
					}
				
				?>
            </div>
Richard MacCutchan 22-Jan-21 3:57am
   
You just need to declare the variable outside the block of code (the if or while block) so it is visible further on. See PHP Variables Scope[^] for an explanation of scope rules.
jennannegraham 22-Jan-21 6:33am
   
okay :) - thank you for this! I do understand and will do this
Richard Deeming 21-Jan-21 9:09am
   
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation / interpolation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]
jennannegraham 21-Jan-21 11:34am
   
totally agree with you. right now, because I do not have knowledge of how to build this type of a project - I am just following along in a project that was created only for tutorial purposes. in order to get some bearing on what needs to happen. before I implement it into my real project - I have plans of using pdo and adding lots more security. this is only so that I can get an idea
Gerry Schmitz 21-Jan-21 11:04am
   
Do you actually have data in "users" and is your "select" retrieving something? I don't see you handling the case where the select return nothing (which could account for your original issues).
jennannegraham 21-Jan-21 11:32am
   
yes, I have 2 test users in the database - and on all the other cases it is working. it is just when the messages page loads that it throws these errors :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900