Connecting Tech Pros Worldwide Forums | Help | Site Map

How do I use a variable to fill a form with MySQL record data?

Newbie
 
Join Date: Apr 2007
Posts: 7
#1: Apr 5 '07
Hello,
I am trying to make a PHP/HTML update script for MySQL.

I prompt the user to enter the ID# of the record they wish to update into a form which then uses the passed ID# to select the associated record from the database:

Expand|Select|Wrap|Line Numbers
  1. $id = @$_GET['q'] ;
Expand|Select|Wrap|Line Numbers
  1. $query = "SELECT FROM Hcffres WHERE residentid = $id";
  2. mysql_query($query);
How do I output the data from the query into the form so the user simply alters the field that needs to be updated? I've tried variations of this:

Expand|Select|Wrap|Line Numbers
  1. Last Name: </font></td><td><input type="text" name="lastname" value="<?php echo $lastname; ?>"  size="40"</td></tr>
but atm I get a blank form and when I try to input any data the residentid = 0 not the passed variable

-- thanks for any help --

ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#2: Apr 6 '07

re: How do I use a variable to fill a form with MySQL record data?


Quote:

Originally Posted by holy moly

Hello,
I am trying to make a PHP/HTML update script for MySQL.

I prompt the user to enter the ID# of the record they wish to update into a form which then uses the passed ID# to select the associated record from the database:

Expand|Select|Wrap|Line Numbers
  1. $id = @$_GET['q'] ;
Expand|Select|Wrap|Line Numbers
  1. $query = "SELECT FROM Hcffres WHERE residentid = $id";
  2. mysql_query($query);
How do I output the data from the query into the form so the user simply alters the field that needs to be updated? I've tried variations of this:

Expand|Select|Wrap|Line Numbers
  1. Last Name: </font></td><td><input type="text" name="lastname" value="<?php echo $lastname; ?>"  size="40"</td></tr>
but atm I get a blank form and when I try to input any data the residentid = 0 not the passed variable

-- thanks for any help --


[PHP]$id = $_GET['q'] ;


$query = "SELECT * FROM Hcffres WHERE residentid = '$id'";
mysql_query($query);
[/PHP]
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#3: Apr 6 '07

re: How do I use a variable to fill a form with MySQL record data?


I usually build an array returning PHP function:

[php]
function query($qry)
{
$res = mysql_query($qry);
$ret = array();
$i = 0;
while($row = mysql_fetch_array($result, MYSQL_BOTH))
if($row)
$ret[$i++] = $row;
return $ret;
}
[/php]
Newbie
 
Join Date: Apr 2007
Posts: 7
#4: Apr 7 '07

re: How do I use a variable to fill a form with MySQL record data?


thanks for the replies but the problem remains:
[php]
$query = "SELECT * FROM hcffres WHERE residentid = $var";
mysql_query($query);
// I need to know what command goes here
echo"<form action=....
[/php]
what is the command I need to output the data returned from the query into the html form so the user can alter the field that needs updating

here's hoping...
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#5: Apr 7 '07

re: How do I use a variable to fill a form with MySQL record data?


Quote:

Originally Posted by holy moly

what is the command I need to output the data returned from the query into the html form so the user can alter the field that needs updating

Did you even attempt to look at my last post? I provided a function which will return the entire dataset in an associative array for easily accessing your MySQL record set.
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#6: Apr 7 '07

re: How do I use a variable to fill a form with MySQL record data?


Quote:

Originally Posted by holy moly

thanks for the replies but the problem remains:
[php]
$query = "SELECT * FROM hcffres WHERE residentid = $var";
mysql_query($query);
// I need to know what command goes here
echo"<form action=....
[/php]
what is the command I need to output the data returned from the query into the html form so the user can alter the field that needs updating

here's hoping...

[PHP]
$query = "SELECT * FROM hcffres WHERE residentid = $var";
$result = mysql_query($query);
$row = mysql_fetch_array( $result );
echo 'other stuffs';
echo '<input type="text" name="name_goes_here" value="'.$row['column_name_here'].'"/>';[/PHP]
Newbie
 
Join Date: Apr 2007
Posts: 7
#7: Apr 9 '07

re: How do I use a variable to fill a form with MySQL record data?


thanks for the replies, I didn't get it to work but I went round the problem.

i saw the function, thanks, but I wanted to know how to fix that specific problem.

of course I have a new problem now with JS form validation
avaialble at: http://www.thescripts.com/forum/show...20#post2492320 if anyone's interested.
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#8: Apr 9 '07

re: How do I use a variable to fill a form with MySQL record data?


Quote:

Originally Posted by holy moly

...
i saw the function, thanks, but I wanted to know how to fix that specific problem.
...

Well, the function I gave you shows exactly how to solve your problem. Your problem was that you were not actually retrieving any of the information after you performed a query.
Newbie
 
Join Date: Apr 2007
Posts: 2
#9: Apr 9 '07

re: How do I use a variable to fill a form with MySQL record data?


Quote:

Originally Posted by Motoma

Well, the function I gave you shows exactly how to solve your problem. Your problem was that you were not actually retrieving any of the information after you performed a query.

you can add wxtract function in the end ;)

$query = "SELECT * FROM hcffres WHERE residentid = $var";

$result = mysql_query($query);

$row = mysql_fetch_array( $result );
extract($row;
echo 'other stuffs';

echo '<input type="text" name="name_goes_here" value="$column_name_here"/>';
Reply


Similar PHP bytes