infill form fields from mysql data base

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
inosent1
Forum Commoner
Posts: 97
Joined: Wed Jan 28, 2009 12:18 pm

infill form fields from mysql data base

Post by inosent1 »

hi

i have a form a user initially fills in, submits, and the data safely gets into the database.

but i want to recall the form, re-fill the fields from the database, edit as needed, then resubmit

as far as the form submission that is working fine. the area i am getting stuck with is pulling the record from the DB and placing the data in the form fields


i had been using the code below, but it makes the form page duplicate (!)

Code: Select all

{

$sql = mysql_query("select * from cdata where files_id='$term'");
while ($row = mysql_fetch_array($sql)){

and then in the form field for the value

Code: Select all

value="<?php echo $row['cb_ytd_ps'];?>"

the data dos show up in the field, but the page duplicates itself for some weird reason
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: infill form fields from mysql data base

Post by Jonah Bron »

You need to use mysql_real_escape_string() in your query. And since you only want one row, use an if statement instead.

Code: Select all

$sql = mysql_query("select * from cdata where files_id='" . mysql_real_escape_string($term) . "'");
if ($row = mysql_fetch_array($sql)) {
And clean the output with htmlspecialchars().

Code: Select all

value="<?php echo htmlspecialchars($row['cb_ytd_ps'], ENT_QUOTES);?>"
http://php.net/mysql-real-escape-string
http://php.net/htmlspecialchars
Post Reply