Forum Moderators: coopster

Message Too Old, No Replies

PHP / SQL get data from SQL db and show it in an HTML form

         

GmanJoe

3:11 pm on Sep 29, 2014 (gmt 0)

10+ Year Member



Hi,
I am very new to PHP and I am trying to accomplish the following. I have an SQL (not mysql) database called RiskProfile which has a table in it called Risk. I also have an HTML form that a user fills in and using php, it stores the data into the database. After some trial and error it now works fine. What I want to do next is to retrieve a record in the db and display it into an HTML form based on the users Social.
so far I have been able to query the db and get results back as an echo but every attempt to display the results in a form field has failed.
Here is what I have that does the echo and works:

I have two forms on one HTML page
Form 1 (riskupdate)is an SSN field in which the user types what SSN they want to find and when the "Find" button is clicked it calls results.php which echos the info to the screen:

<form name="riskupdate" method="GET" action="results.php">
<p align="left">Find SSN:&nbsp; <input type="text" name="FindSSN" size="9"></p>
<p align="left"><input type="submit" value="Find" name="B3">&nbsp;&nbsp;
<input type="reset" value="Reset" name="B4"></p>
</form>

Form 2 (riskprofile)this is where I would like the database results to be displayed in the proper fields but have no idea how to do it:

<form name="riskprofile" id="riskpro" method="GET" >
<p align="left">First Name: <input type="text" name="FrstName" id="FrstName" size="50" tabindex="1">&nbsp;&nbsp;
</p>
<p align="left">Last Name: <input type="text" name="LstName" size="50" tabindex="2">&nbsp;&nbsp;&nbsp;
</p>
<p align="left">SSN: <input type="text" name="SocialSecNum" id="SocialSecNum" size="9" tabindex="3"> (No
dashes)</p>
</form>

and here is the code from results.php which currently gets the info and just echos it to the screen insted of into the form riskprofile

<?php
if(! ini_get('date.timezone'))
{
date_default_timezone_set('America/New_York');
}

$serverName = "RSBSQL";
$connectionInfo = array( "Database"=>"RiskProfile", "UID"=>"myDBid", "PWD"=>"myDBpassword");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
die( print_r( sqlsrv_errors(), true ));
}
/*Retrieve data.*/
$find = $_GET['FindSSN'];
$sql = "SELECT * FROM Risk WHERE SSN=$find";


$stmt = sqlsrv_query( $conn, $sql);

if($stmt === false)

{

$errors = sqlsrv_errors();

die(print_r($errors, true));

}

else

{

while($rows = sqlsrv_fetch_array($stmt))
{
echo $_GET['SocialSecNum']=$rows['SSN'];
echo $_GET['FrstName']=$rows['FirstName'];
echo $_GET['LstName']=$rows['LastName'];
}


}


sqlsrv_close( $conn);
?>

Any help you could give would be great.

Dammy

9:13 am on Sep 30, 2014 (gmt 0)

10+ Year Member



Hi, u cn acomlish dis on dat html page by just querying d db an echo the result in the value="" attribute of the form.

GmanJoe

3:39 pm on Oct 1, 2014 (gmt 0)

10+ Year Member



Well.. I managed to figure this one out. First I broke the 2 forms on one HTML page into 2 HTML pages, 1 page for each form. Then when the first HTML page was submitted, I had PHP store the vaules in a session, i renamed my second html page into a php page and had it retrieve the values from the session. then using Dammy's suggestion i used the value attribute to read the session values.