0
votes

I'm kinda new to php and since I started using it I encounter this error Notice: Undefined index: title_1 I connected the table

$ind_header = $db->query("SELECT * FROM header_index")->fetchAll(PDO::FETCH_ASSOC);

I try <?php echo $ind_header['title_1']; ?> I get that error, but when I use a foreach loop it works. Now I wanna show the results of the database in a form value like this

<form action="<?php echo BASE_URL ?>/admin/admin.php" method="POST">
    <label>Title one</label>
    <input type="text" class="form-control" name="title_1" placeholder="Full name" value="<?php echo $ind_header['title_1']; ?>">

I get this full error

Notice: Undefined index: title_1 in D:\wamp\www\translate\app\views\cms\body_views\header.php on line 11 Call Stack #TimeMemoryFunctionLocation 10.0010132416{main}( )..\admin.php:0 20.0060141696require( 'D:\wamp\www\translate\app\views\admin\admin.php' )..\admin.php:4 30.0070145864include( 'D:\wamp\www\translate\app\views\cms\body.php' )..\admin.php:3 40.0070149056include( 'D:\wamp\www\translate\app\views\cms\body_views\header.php' )..\body.php:7 ">

1
->fetchAll() returns all rows in a multidimentional array, so that is why a foreach loop allows you to access title_1. If you only want the 1st value, either change the ->fetchAll() to ->fetch() OR you can do $ind_header[0]['title_1'] where [0] represents the row you want the title_1 value from - Sean
@Sean Now it worked, thank you for that. - Bououm

1 Answers

0
votes

fetchAll() returns all data in multidimensional array. To get data you need to use

$ind_header[0]['title_1']

When you use foreach() it is automatically handled by system.