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
• 8,730 points
84 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,280 points

Related Questions In Others

0 votes
1 answer
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
247 views
0 votes
1 answer

Avoiding memory leaks in C++

If you use smart pointers at all ...READ MORE

answered Nov 5, 2018 in Others by nirvana
• 3,130 points
797 views
0 votes
1 answer

Events & Threading in Windows Forms

I don't think you should be updating ...READ MORE

answered Nov 10, 2018 in Others by nirvana
• 3,130 points
297 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
4,482 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
248 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,500 points
381 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,280 points
26 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP