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, 2022 in Others by Soham
• 9,710 points
3,423 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, 2022 by Aditya
• 7,680 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, 2022 in Others by gaurav
• 23,260 points
1,669 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, 2022 in Others by narikkadan
• 63,600 points
1,624 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, 2022 in Others by narikkadan
• 63,600 points
722 views
0 votes
1 answer

Copy last 3 rows, excluding the rows for which there is a "0" in column "C"

The copy inside the loop is overwriting ...READ MORE

answered Jan 10, 2023 in Others by narikkadan
• 63,600 points
467 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
10,540 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,740 points
829 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,520 points
934 views
0 votes
1 answer

"Server Tomcat v7.0 Server at localhost failed to start" without stack trace while it works in terminal

To resolve this issue, you have to ...READ MORE

answered Feb 23, 2022 in Others by Aditya
• 7,680 points
3,187 views
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, 2022 in Others by Aditya
• 7,680 points
714 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