Page 1 of 1

infill form fields from mysql data base

Posted: Fri Jun 03, 2011 3:44 pm
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

Re: infill form fields from mysql data base

Posted: Fri Jun 03, 2011 5:44 pm
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