Callum Marshall
Callum Marshall

Reputation: 1

Populating HTML form from MySQL using PHP

I've successfully populated a HTML form dropdown menu using this method, so I know it works, the difference this time is I'm trying to populate text boxes with content (account details), but it's just not working.

PHP:

try {
        $sql = "SELECT Firstname, Surname, Email FROM `users`;";
        $result = $pdo->query($sql);


    }
    catch (PDOException $e){
        echo "There was an error whilst fetching the users information:" .$e->getMessage();
        exit();
    }

    while($row=$result->fetch()){
        $displayinfo[] = array('UserFirstName' => $row['Firstname'],
            'UserSurname' => $row['Surname'], 
            'UserEmail' => $row['Email']);
    }

HTML SIDE:

<?php foreach($displayinfo as $info): ?>
                <?php echo "<input type="text" name="firstname" value=". $info['UserFirstName'] ."><br>"
                            ?>
                <?php endforeach; ?>    

This seems to show a

Syntax error, unexpected T_String, expecting ',' or ';'

Any help would be appreciated

Upvotes: 0

Views: 46

Answers (2)

Emanuel Oliveira
Emanuel Oliveira

Reputation: 56

You should escape double quotes with backslash inside of input.

Ex: echo "<input type=\"text\"/>".

Or use single quotes.

Ex: echo '<input type="text"/>'.

Upvotes: 1

Scriptman
Scriptman

Reputation: 432

The problem is in:

<?php echo "<input type="text" name="firstname" value=". $info['UserFirstName'] ."><br>"

You need to use the single '

<?php echo '<input type="text" name="firstname" value='. $info['UserFirstName'] .'><br>';

Upvotes: 1

Related Questions