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>