Page 1 of 1
displaying mysql data on html form
Posted: Thu Oct 08, 2009 10:31 am
by dkperez
After 2 hours of using the title of this topic to search the Internet, and getting about a million hits that tell me how to insert data in the database, I'm trying here......
I come from the world of DEC Rally, Oracle Forms, MS Access, etc, so I think I'm struggling because this seems like it should be trivial, but....
I have a simple form in html - has a first name (firstname) and a last name (lastname), and a "Search" button. My action is calling "getdata.php" to retrieve data from a mysql database.
All I want to do is display the empty form. Hit the Search button. Have the php retrieve all the records from a database table. Then display the same form with first retrieved record filled in. Once I've got data displayed I need a way to walk forward and backward through the records (next and previous buttons?), make changes to fields, and update the database.......
Standard, dull, boring, form transaction processing...
My php connects to the database, runs the query, and I can echo the values in the php.
My disconnect is HOW do I put the individual fields from my query result into the form fields, and display the form with the data visible? I'm missing some piece of syntax.
Is there a complete, idiot-proof tutorial somewhere that goes through this is detail 'cause the php book I've got appears not to cover how to do the standard select/insert/update/delete processing from the form...
Re: displaying mysql data on html form
Posted: Thu Oct 08, 2009 12:30 pm
by JNettles
Code: Select all
<input type="text" value="<?php echo $queryresult['name']; ?>" />
Is that what you're looking for? Just dump the variable holding your results into the value property of the textbox.
Re: displaying mysql data on html form
Posted: Thu Oct 08, 2009 3:35 pm
by dkperez
OK, I'm sort-of following you....
In my "original" form that displays empty and lets the user hit the "search" button (with or without some field
or fields filled in, which I've figured out how to do), my fields were defined as:
<div>
<label style=float:left for="firstname">First Name:
<input id="firstname" name="firstname" type="text" class="field text" value="" size="40" tabindex="1"/>
</label>
<label class=right for="lastname">Last Name:
<input id="lastname" name="lastname" type="text" class="field text" value="" size="40" tabindex="5" />
</label>
</div>
Do I just replace the fields on the original form with what you showed? Create a new html form? Or is this something that gets done in php and somehow knows how to do this? I'm missing some kind of connection in this whole thing 'cause I know how I got into the php that retrieves the data from my original form, but how do I get BACK to my form with the data displayed? I"m good at databases, and somewhat good at html, but I'm still a newbie at php, and the book I have isn't much help...
Re: displaying mysql data on html form
Posted: Thu Oct 08, 2009 4:35 pm
by ceiroa
There are many ways to do this, but the simplest (although not very scalable) is to have everything in one file. In this case the form’s action tag is set to itself. If the name of the form was testform, you were using POST, and there was only one field in the form (username) it’d look like this:
Code: Select all
<form name="input" action="testform.php" method="post">
Username: <input type="text" name="user" />
<input type="submit" value="Submit" />
</form>
When the form is submitted the request goes to the same page . Then you can have PHP at the top of the page that gets the POST request and retrieves the value from it:
You can have an “if” after that. If the value is empty the code will show the form. If it isn’t it will retrieve the data from the database for the matching record (in this case the record from table users where user is equal to the string entered in the form), and display it. Remember that PHP can echo anything you want to the page, including HTML, and that you can mix PHP and HTML in the page at leisure. Any basic PHP book should explain how to do that.
(A much better solution would be to use the MVC controller, and AJAX to update the page without refreshing the whole thing, but that gets a little bit more complicated.)
------
Carlos R. M. Eiroa
http://www.soliantconsulting.com/apps/weboss/
Re: displaying mysql data on html form
Posted: Fri Oct 09, 2009 12:06 am
by dkperez
OK, I'm starting to get it a little...........
By dumb luck I stumbled on how to display the values on the form..... In the php code I have to assign a value from mysql_result (or perhaps I'll eventually get the mysql_fetch_array to actually work). THEN, in the html code where the
"input id" is, if I add the "<?php echo $firstname; ?>" it'll show up......
So, now the problem moves...
Can I put the php in a separate file and still have everything use the displayed form? If so, how?
I want to display the form initially and not run a php query (which I presume it does now since the php is at the top of the file, above the html)... THEN, let the user enter a field (or none as they wish) THEN run the php to get the data.
Currently, if I let it retrieve 10 records, I see the data from the first one on the form, but have no way to move through the records...
More importantly, I want to have this thing act like a real transaction processing app... Retrieve all the data based on search criteria; display the form with the first record, let the user move forward and backward through records, make changes as desired, and have those changes update the database. Of course, if any change is made to database data that was previously retrieved, the RI should ensure that a new query is performed so the data is accurate...
I presume this is possible with php and html (yes?), and if so, is there a good example somewhere of how to move through records, make changes, and ensure that the displayed data is always synchronized with the database data?
Re: displaying mysql data on html form
Posted: Fri Oct 09, 2009 10:15 am
by ceiroa
You are asking too many things at the same time. In order to display the form only at the beginning (before the user submits) start by using the "if" condition that I mentioned:
Code: Select all
$username = $_POST[‘user’];
if($username == "") {
echo '<form name="input" action="testform.php" method="post">
Username: <input type="text" name="user" />
<input type="submit" value="Submit" />
</form>';
} else {
//Make a query to the database and display here the result
}
Re: displaying mysql data on html form
Posted: Fri Oct 09, 2009 10:02 pm
by dkperez
OK, I'm slowly and painfully making (maybe) progress.......
With all your help I've finally gotten the form to display, let me hit the button and walk through the records...
I'm using the brute force method of record retrieval
select * from yyy limit startrow, 1;
which is normally a bozo no-no 'cause the database retrieval is so abysmally slow, but I'll work on getting the thing to do a single retrieval followed by some way to walk through the returned array forward and backward.......
I'm sure I'll be back with more questions, but for now I'm heading off on vacation......
I have to say, I'm hoping this whole php/html/web stuff gets a lot esaier, 'cause right now I'm feeling like I've taken a step back about 20 years...