Jump to content

Undefined Offset Notice Warning


Glese

Recommended Posts

Here is an example of the beginning of a rating system script:

 

// RATING SYSTEM - START

if ((isset($array[0]) || isset($array[1])) && isset($_SESSION['user_id'])) {

// check to see if user has already voted on given contribution
$query = "SELECT * FROM votes WHERE con_id = '$array[2]' AND user_id = '$array[3]'";
$query_run = mysqli_query ($dbc, $query) or die (mysqli_error($dbc)); 
$num_rows = mysqli_num_rows ($query_run);
$assoc = mysqli_fetch_assoc ($query_run);
$rating = $assoc['rating'];


// check to see if it's user's own contribution
$query = "SELECT * FROM con WHERE con_id = '$array[2]'";
$query_run = mysqli_query ($dbc, $query) or die (mysqli_error ($dbc));
$assoc = mysqli_fetch_assoc ($query_run);
$con_user_id = $assoc['user_id'];

 

 

For every array variable I am getting the error message "Undefined Offset".

 

Any ideas how to solve this one, I do not fully know yet what is common practice in these cases. The warnings also do not always happen, they usually happen, they happen in a specific moment when I use a drop down menu, to show table content.

 

Link to comment
Share on other sites

if ((isset($array[0]) || isset($array[1])) && isset($_SESSION['user_id'])) {

// check to see if user has already voted on given contribution
$query = "SELECT * FROM votes WHERE con_id = '$array[2]' AND user_id = '$array[3]'";

That section checks for array elements zero and one, and then uses array elements two and three.  If we had the actual error message, we might be able to tell if that's the problem.

Link to comment
Share on other sites

This is the actual error message:

Notice: Undefined offset: 2 in C:\xampp\htdocs\php_projects\myproject\controller\rating_system\rating_system_func.php on line 14

Notice: Undefined offset: 3 in C:\xampp\htdocs\php_projects\myproject\controller\rating_system\rating_system_func.php on line 14

Notice: Undefined offset: 2 in C:\xampp\htdocs\php_projects\myproject\controller\rating_system\rating_system_func.php on line 22

Notice: Undefined offset: 2 in C:\xampp\htdocs\php_projects\myproject\controller\rating_system\rating_system_func.php on line 64

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1[b][/b]

Link to comment
Share on other sites

Array comes from this function, you can see array at the very bottom:

function knuffix_list ($query, $dbc) {

$data = mysqli_query ($dbc, $query) or die (mysqli_error ($dbc));						

// Loop through the array of data
while ($row = mysqli_fetch_array ($data)) {

	global $array;
                

                

	// Variables for the table
	$con_id = $row['con_id'];
	$likes_count = $row['likes'];
	$dislikes_count = $row['dislikes'];
	$dbuser_name = $row['nickname'];
	$user_id = $row['user_id'];
	$contribution = $row['contribution'];	
	$title = $row['name'];
                
                $avatar_file_name = $row['avatar'];
                $avatar_folder = "../profile/avatar/";

               
                  
                
                
                
                // The TABLE

                include('character_artwork_table.php');
                    
                
   
                
                
                
                
                // POST BUTTONS inside the table
                if (isset($_POST['likes']))
	$likes = $_POST['likes'];
                if (isset($_POST['dislikes']))
	$dislikes = $_POST['dislikes'];
                if (isset($_POST['hidden_con_id'])) {
	$con_id = $_POST['hidden_con_id'];
	//$favorite = $_POST['favorite'];
                }
                
               
	// $array = array ($likes, $dislikes, $con_id, $user_id);
                
                if (isset($likes)) 
                    $array[] = $likes;
                if (isset($dislikes))
                    $array[] = $dislikes;
                if (isset($con_id))
                    $array[] = $con_id;
                if (isset($user_id)) {
                    $array[] = $user_id;
                }

}

mysqli_close($dbc);

}

Link to comment
Share on other sites

So at least two of those are unset.  See how that works?  Your array is randomly one or more of those 4 items, yet your function assumes all 4 will be there.

 

 

 

 

             if (isset($likes)) 
                    $array[] = $likes;
                if (isset($dislikes))
                    $array[] = $dislikes;
                if (isset($con_id))
                    $array[] = $con_id;
                if (isset($user_id)) {
                    $array[] = $user_id;
                }

 

 

What I do not understand is, the code is stating IF it is set, do assign it to the array, so if it is not set, do not assign it to the array, in this sense, I should not get the notice, as I stated I usually, do not get the notice only in a specific situation, when I do choose certain entries from a drop down menu, and with other entries I do not get the error.

Link to comment
Share on other sites

You're exactly right about the first part, then you get wrong.

 

When you create the array, you do an isset call and only add to the array when the item is set.  Yes, exactly right.

 

Then you attempt to use the fourth element in the array.  What happens if that's not set?  You get the error.

 

Also note that this code has no way of telling which variable is which in this array.  You assume that $array[2] is con_id.  What if $dislikes isn't set?  Then con_id will be in $array[1]

 

You need to rethink this whole thing and look into naming your array keys, so you have $array['con_id'] instead of $array[2]

Link to comment
Share on other sites

 

 

You need to rethink this whole thing and look into naming your array keys, so you have $array['con_id'] instead of $array[2]

Is that what you would recommend in general, or only specifically in this case?

 

Thanks for the recommendation though, I did not even notice that one.

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.