PHP undefined variable when I try to extract data from database

Panha Phal

So I got these issues when I tried to extract data from the database. My DB connection is working fine. It is showing "Database connection established" and inside my index.php I wrote a for each loop to get the data and inside my HTML code, I display it inside the table. I got these errors:


Notice: Undefined variable: jokes in C:\xampp\htdocs\comp1321_database\jokes\jokes.html.php on line 16

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\comp1321_database\jokes\jokes.html.php on line 16

Here is the HTML and php code to display the data:

<?php include_once 'admin/includes/helpers.inc.php';?>

<!DOCTYPE html>
<html lang="en">
<head>
     <meta charset="utf-8">
     <title>List of Jokes</title>
</head>
<body>
<p><a href ="/comp1321_database/jokes/form.html.php/">Add your own joke</a></p>     
<p>Here are all the jokes in the database</p>
<!-- into a table -->
        <table border="1">
        <?php foreach ($jokes as $joke): ?>
            <!-- <form action="?deletejoke" method="post">  -->
             <tr>
             <td><?php html($joke['joketext']);?></td>
             <td><?php $display_date = date("D d M Y", strtotime($joke['jokedate']));
             html($display_date); ?>
         </td>
         <td><img height="100px" src="images/<?php html($joke['image']);?>"
            /></td>
            <td><input type="hidden" name="id" value="<?php echo $joke['id'];
            ?>">
            <input type="submit" value="Delete"></td>
        </tr>
    <!-- </form> -->
        <?php endforeach; ?>
        </table>
   <?php include 'admin/includes/footer.inc.html.php';?>   
</body>
</html>   

and here is the index.php:



<?php
// selection block
include  'admin/includes/db.inc.php';
// 
try
{
    $sql = 'SELECT * FROM joke';
    $result = $pdo->query($sql);

}  catch (PDOException $e) {
    $error = 'Error fetching jokes' . $e->getMessage();
    include 'error.html.php';
    exit();
}
foreach ($result as $row) {
    $jokes[] = array(
        'joketext'=> $row ['joketext'],
        'jokedate'=> $row['joketext'],
        'image'=> $row['image']
    );

}
 include 'jokes.html.php';
?>

Many thanks.

Wesley Smith

Initialize your jokes variable before you try to use it, ie:

$jokes = [];

try
{
    $sql = 'SELECT * FROM joke';
    $result = $pdo->query($sql);

}  catch (PDOException $e) {
    $error = 'Error fetching jokes' . $e->getMessage();
    include 'error.html.php';
    exit();
}
foreach ($result as $row) {
    $jokes[] = array(
        'joketext'=> $row ['joketext'],
        'jokedate'=> $row['joketext'],
        'image'=> $row['image']
    );

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

When I try to echo data from database using php my html page breaked

Got undefined when I try to reach data from node API

PHP error message when I try to check a value from database

Undefined json data when i try to call it

Why do I get an error (Notice: Undefined index) when I try to echo a value from my database?

I get 'undefined' when i try to fetch data from an API but when i access it again it works. why?

Undefined variable in laravel when i try to send mail

What happen if I try to get sizeof an undefined variable? (PHP)

C# datagrid duplicate the columns when I try to get the data from database table

Jmeter : Error when I try to get data from Database using beanshell assertion

KOTLIN/FIREBASE Error when I try get data from firebase database - Client is offline

I am getting an "Undefined variable" error in CodeIgniter when fetching from database

PHP Exits When I Try to Connect to a Database Using MySQLi

App crashes when I try to insert data into database

App crashes when i try to add data to my database

Retrieve Data From Database And Store It In A Variable In PHP

variable undefined when get data from json_encode (Function PHP)

Undefined variable: tags, display data from database on bootstrap modal

Getting Undefined variable while trying to retrieve user data from database

Firebase data retrive from database only in login activity when i try to access the current user in any other activity it gives null

Cannot extract all data from MySQL database using PHP

When I get data in the variable from sharedprefs and firestore database It won't displayed on screen

when retrieving data from firebase database the browser shows undefined

Undefined index when printing data from mysql database?

Error when I try to extract info in a json

Can I use variable in python to extract data from Postgresql?

ViewModel return clear object when i try to get data from it

NullPointerException when i try Select data from Mysql base

404 ERROR when I try to GET data from google spreadsheet

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive