Reputation: 2413
I have a database that is filled using a form, but I wanted to be able to retrieve the fields back and fill in the form so they don't have to retype when editing.
How can I do this using PHP and MySQL?
For example I select all my columns and then I have corresponding text and check boxes on my form in my HTML file after I select them from the database. How would I put them into those boxes?
Upvotes: 1
Views: 12031
Reputation: 3185
First, connect to the database.
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
Then query the database with an SQL query.
$result = mysql_query($query);
Then pull out the individual data items and echo them where you need.
while ($row = mysql_fetch_assoc($result)) {
echo $row['firstname'];
echo $row['lastname'];
echo $row['address'];
echo $row['age'];
}
Upvotes: 0
Reputation: 6773
You'll need to render the form with the fields filled in. For example, the "value" attribute is used to specify the initial state of the form element.
<form>
First name: <input type="text" name="firstName" value="<?php echo $firstname; ?>" /><br />
Last name: <input type="text" name="lastName" value="<?php echo $lastname; ?>" /><br />
<input type="submit" value="Submit" />
</form>
Upvotes: 3
Reputation: 490647
value
attribute, determine which option
should be selected, what checkbox should be checked.htmlspecialchars()
for value
attributes.Upvotes: 1