Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying Mysql querie in a form

Status
Not open for further replies.

jokobe

Programmer
May 7, 2003
6
DE
I want to display a Mysql Querie in a form.
Like this:
First show a list of all records. Then, pressing the
edit link, a single record is displayed in a form.
I tried this way
- "Describe my_table" to get the column names
- build a form and echo the values.
I should work for different tables. But it is not working.

 
If I understand you correctly, you are trying to display the records of the table. DESCRIBE table_name will, as you yourself said, will get you column names. I am no sure why you need column names, except, maybe, for table header. At any rate, you would also need the query to actually get the values. Do this to get all records:

SELECT * from table_name;

Then you can print out a row for each entry in the result set. For the edit , you can make some value in each row a link, passing the unique identier (like an index for the entry) to a function which would then do:

SELECT * from table_name WHERE unique_id = "$unique_id";

and put the resultant set into the form you talked about. Upon submit, the following would run:

REPLACE INTO table_name (column1,column2,column3,...) VALUES(value1,value2,value3,...) WHERE unique_id = "$unique_id";
 
Thanks for your answer. Seems I haven't explained properly.
I'll display a list of records via a SQL query. The user hits the edit link and the record should be displayed in a HTML form for editing. I fetched the "describe" query to get the columns name and build a string like
<input type="text" name="var_name" value=<?php echo $var_name ?>">.
Var_name is depending on the table I'm querying, the might be four or six or .. columns in the table.
 
TheNOt too sure what you want to do but, if you want to dynamically name the form fields using the Describe you can.

Describe command however outputs a lot of info thet you do not need; what you need is just the "Field" column; you can use it as you would with any other table/column.

$sql = "describe table_name";
$exc = mysql_query($sql, $conn) or die(mysql_error());

while ($newArray = mysql_fetch_array($exc)) {

$fld = $newArray['Field'];

print "<input type=\"text\" name=\"$fld\">

}


Cheers


QatQat




Life is what happens when you are making other plans.
 
Thanks a lot.
Yes I want dynamically name the input fields and
dynamically fill the values for the input field.
<name=\"$fld\"> is quiet clear now to me, thanks. But what to fill into the value field? I have something like a
query: "Select * from table_name where ..." and have to post the
values.
$sql = "Select * from table_name where ..." ";
$exc = mysql_query($sql, $conn) or die(mysql_error());

while ($anothernewArray = mysql_fetch_array($exc)) {

$anotherfld = $anothernewArray['Field'];
print "<input type=\"text\" name=\"$fld\" value=\"<?php
echo $anotherfld ?>\">

Guess I'll try that and thanks a lot again.
 
If you do not have too many tables/fields to edit, I suggest that you name the form's text fields in your HTML and just pull the values from the database.


anyway, if you really need to do it dynamically

$sql2 = "Select * from table_name where ..." ";
$exc2 = mysql_query($sql2, $conn) or die(mysql_error());

while ($anothernewArray = mysql_fetch_array($exc2)) {

$anotherfld = $anothernewArray['ColumnName'];
print "<input type=\"text\" name=\"$fld\" value=\"<?php
echo $anotherfld ?>\">


Bye

Qatqat


Life is what happens when you are making other plans.
 
Thanks a lot.
It is now working.

Now I'm going back to fight for editing the recordset *LOL*


Jokobe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top