Notice Undefined offset 0 in

0 votes

I am getting this PHP error and do not know what it means?

Notice: Undefined offset: 0 in C:\xampp\htdocs\mywebsite\reddit_vote_tut\src\votes.php on line 41

From this code:

<?php 
include("config.php"); 

function getAllVotes($id) 
{ 
        $votes = array(); 
        $q = "SELECT * FROM entries WHERE id = $id"; 
        $r = mysql_query($q); 
        if(mysql_num_rows($r)==1)//id found in the table 
{ 
        $row = mysql_fetch_assoc($r); 
        $votes[0] = $row['votes_up']; 
        $votes[1] = $row['votes_down']; 
} 
return $votes; 
} 

function getEffectiveVotes($id) 
{ 
                $votes = getAllVotes($id); 
                $effectiveVote = $votes[0] - $votes[1]; //ERROR THROWN HERE 
                return $effectiveVote; 
} 
$id = $_POST['id']; 
$action = $_POST['action']; 

//get the current votes 
$cur_votes = getAllVotes($id); 

//ok, now update the votes 

if($action=='vote_up') //voting up 
{ 

      $votes_up = $cur_votes[0]+1; //AND ERROR THROWN HERE 

      $q = "UPDATE threads SET votes_up = $votes_up WHERE id = $id"; 
} 
elseif($action=='vote_down') 
{ 

      $votes_down = $cur_votes[1]+1; 
      $q = "UPDATE threads SET votes_down = $votes_down WHERE id = $id"; 
}

$r = mysql_query($q); 
if($r) 
{ 

          $effectiveVote = getEffectiveVotes($id); 
          echo $effectiveVote." votes"; 
} 
elseif(!$r) //voting failed 
{ 
        echo "Failed!"; 
} 
?>
Feb 16 in Others by Soham
• 9,670 points
638 views

1 answer to this question.

0 votes

 I assume you are looking for the values at key 0 of $votes which is an array that does not contain that key. The array $votes is not set, so when PHP is trying to access the key 0 of the array, it encounters an undefined offset for [0] and [1] and throws the error.

If you have an array:

$votes = array('1','2','3');

We can now access:

$votes[0]; $votes[1]; $votes[2];
If we try and access:

$votes[3];

We will get the error "Notice: Undefined offset: 3"

answered Feb 16 by Aditya
• 7,660 points

Related Questions In Others

0 votes
1 answer

AWS VPC CIDR in both 10.0.0.0/8 and 192.168.0.0/16 [closed]

The Amazon Virtual Private Cloud (Amazon VPC) ...READ MORE

answered Mar 21 in Others by gaurav
• 22,040 points
346 views
0 votes
1 answer

Subtotal in result row returns 0 in excel table

Syntax SUBTOTAL(function_num,ref1,[ref2],...) The SUBTOTAL function syntax has the following ...READ MORE

answered Sep 25 in Others by narikkadan
• 37,660 points
65 views
0 votes
1 answer

Excel issue "excel 4.0 function stored in defined names"

I don't want to revive an old ...READ MORE

answered Nov 5 in Others by narikkadan
• 37,660 points
20 views
0 votes
1 answer

Reducing sequences in an array of strings

I've written a C# app to solves ...READ MORE

answered Nov 2, 2018 in Others by DataKing99
• 8,240 points
314 views
0 votes
2 answers

Define a SQL query? What is the difference between SELECT and UPDATE Query? How do you use SQL in SAS?

HI.. SQL is Structured Query Language, which is ...READ MORE

answered Aug 8, 2020 in PHP by anonymous
5,478 views
0 votes
1 answer

Vertically centering text in a div

Try this code: .smallUnitBox { ...READ MORE

answered Sep 3, 2018 in Blockchain by digger
• 26,720 points
310 views
0 votes
1 answer
0 votes
0 answers

Anyone can help me out to understand the semantic of (document.getElementBYId("demo").innerHTML="Hello") ?

Hello guys, Can Someone helps me to find ...READ MORE

Jan 17, 2020 in Web Development by anonymous
• 37,510 points
472 views
0 votes
1 answer
0 votes
1 answer

How do I get the current date and time in PHP?

The time would go by your server ...READ MORE

answered Feb 16 in Others by Aditya
• 7,660 points
113 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP