r/PHPhelp • • Dec 17 '20

Solved Hey got a problem with a Undefined array key even though the code works, can s1 help me pls

my website shows me that even tough it collects all the data from the database

it fills the table with the information i need but above the table its says undefinded array key

can s1 pls help me with that problem? sorry for my english and thanks ahead

Warning: Undefined array key "ReviewDate" in D:\XAMPP1\htdocs\sample\index.php on line 228
Warning: Undefined array key "Comment" in D:\XAMPP1\htdocs\sample\index.php on line 229
Warning: Undefined array key "Title" in D:\XAMPP1\htdocs\sample\index.php on line 230 

// this is my code for the table on my index.php site
        
        require_once("methoden.php");

        $recent_reviews = latestreviews();    

<?php
            foreach($recent_reviews as $review)
            {

                $Datum = $review["ReviewDate"];
                $Kommentar = $review["Comment"];
                $Name = $review["Title"];

                unset($review);

                echo "<tr>";
                echo "<td>$Datum</td>";
                echo "<td>$Name</td>";
                echo "<td>$Kommentar</td>";
                echo "</tr>";
            }
                
?>

// this is the code for the function on method.php

function latestreviews()
{

    $db = new DataBase();
    
    $db -> connect();
    
    $sql =  "SELECT artworks.ArtWorkId, reviews.ReviewDate, reviews.Comment,     	         artworks.Title From artworks INNER JOIN reviews ON artworks.ArtworkId = reviews.ArtworkId Order By reviewDate DESC LIMIT 2";
   
    $statement = $db ->prepareStatement($sql);
    $statement -> execute(); 
    

    $reviews[] = array();

    while($review = $statement->fetch() )
    {
        $Datum = $review["ReviewDate"];
        $Kommentar = $review["Comment"];
        $Name = $review["Title"];

        $reviews[] = array ("ReviewDate" => $Datum, "Comment" => $Kommentar, 
                               "Title" => $Name);
    }

    $db->close();
    
    return $reviews;
}
8 Upvotes

7 comments sorted by

3

u/so_O Dec 17 '20

You can change lines 228, 229, and 230 to have a fallback so that if $review isn't an array with those keys set it'll use empty string instead:

$Datum = $review["ReviewDate"] ?? "";
$Kommentar = $review["Comment"] ?? "";
$Name = $review["Title"] ?? "";

1

u/StevenBaecker Dec 17 '20

yes that worked, thank you

1

u/ReportIll1936 Mar 06 '21

Thanks. Helps me a lot.

1

u/tanmaryjoy Oct 17 '21

also have the same problem :<

1

u/tanmaryjoy Oct 17 '21

?? ""

nvm it worked.

1

u/Dhani_Arellano Apr 24 '22

yeeeah! that worked, ty bro

2

u/nudi85 Dec 17 '20

Du solltest deinen Variablen englische Namen geben.