I am a PHP newbie who has just studied Sitepoint’s ‘PHP & MySQL Novice to Ninja’ (5th Ed 2012) by Kevin Yank. I am trying to put it all into practice by building a member-only site.
My php code starts off by offering a login page with a link to a registration page. The registration page has a form of 9 fields in which visitors enter their personal information, which then writes the values to a single 9-column table in the database using a prepared statement. I want to be able to say, “You registered with the following details:…”, and then list them with perhaps the opportunity to edit any mistypes.
However I’ve hit a problem in trying to perform a retrieval of the entered values and present them back to the visitor after registration.
I am trying to use code similar to Kevin Yank’s in Chapter 4 with a SELECT database query to retrieve the values (he was retrieving rows of one field for many entries, I am retrieving all the fields for one row/entry for the most recent id entry), then using the query method to return a result set as a PDOStatement object ($results), which should contain all the fields for the last registrant. There is then a while loop and a call to the fetch method, which should return all the fields as an array called $regdetails. Then I direct to the output file (as an include) which should use a foreach loop to output the elements in the array.
However I keep getting an ‘undefined variable regdetails’ notice and an ‘invalid argument supplied for foreach’ for the following line every time it reaches the included output file.
<?php foreach ($regdetails as $item): ?>
Can anybody suggest where the problem might be, please?