Forum Moderators: coopster
<?php
require ("config.php");
// create loop
$Query = "SELECT * from $TableName"; // set query to get everything from the db
$Result = mysql_db_query ($DBName, $Query, $Link);
print ("<form action=\"HandleForm.php\" method=POST>");
while ($Row = mysql_fetch_array ($Result))
{
// begin for each loop
foreach($Row as $value) {
print ("$value <textarea name=\"Array[$value]\" rows=5 cols=40></textarea><br>\n\n");
}
}
mysql_close ($Link);
print ("<input type=SUBMIT name=\"SUBMIT\" value=\"Submit!\"></form>");
?>
No, the $rows[] array contains all your row names. Much the same as the original while loop. When the loop runs, it adds the value of each row to the array. The foreach loop displays the array.
Give it a try and you`ll see how it works.
<?php
require ("config.php");
// create loop
$Query = "SELECT * from $TableName"; // set query to get everything from the db
$Result = mysql_db_query ($DBName, $Query, $Link) or die(mysql_error());
print ("<form action=\"HandleForm.php\" method=POST>");
while ($Row = mysql_fetch_array ($Result))
{
$rows[] = $Row[];
}
foreach($rows as $value)
{
print ("$value <textarea name=\"$value\" rows=5 cols=40></textarea><br>\n\n");
}
mysql_close ($Link);
print ("<input type=SUBMIT name=\"SUBMIT\" value=\"Submit!\"></form>");
?>
Do you want SQL/table fields?
No. I am having a hard time understanding the point of this whole script so I will go through it line by line. It would also help me if you could explain verbally what you want this little script to accomplish.
require ("config.php");
I assume that connect statement and the select_db statement are in there
$Query = "SELECT * from $TableName"; // set query to get everything from the db
Ok so you are selecting everything from the db. Is there only 1 row in there? If not you may need to add a primary key and use that in a where clause. That will depend on what you are actually doing with this form. If you just want any 1 row to find out your column names then use LIMIT 1.
$Result = mysql_db_query ($DBName, $Query, $Link) or die(mysql_error());
don't use mysql_db_query use mysql_query and select the proper db using mysql_select_db after you connect. You also don't need to specify the $link in each call because it will use the last opened and I hope you only have one open. So change the above line to $Result = mysql_db_query ($Query) or die(mysql_error());
print ("<form action=\"HandleForm.php\" method=POST>");
ok printing the form tag
while ($Row = mysql_fetch_array ($Result)) {
$rows[] = $Row[];
}
now i am lost. Are we outputting a form to the browser for each row in the db? I also think that $rows[] = $Row[]; should be $rows[] = $Row; That will pop the row into your array and ultimately create a multi dimensional array with all of your data returned from your query.
foreach($rows as $value) {
print ("$value <textarea name=\"$value\" rows=5 cols=40></textarea><br>\n\n");
}
This all depends on what exactly you are trying to do. I have some ideas but it would be better if you could clarify what you are actually trying to do. This may work for the first iteration but it will probably blow up on the second because you are building a multidimensional array and will have to reference more this way
foreach($rows[$somecounter] as $value) {
mysql_close ($Link);
print ("<input type=SUBMIT name=\"SUBMIT\" value=\"Submit!\"></form>");
close the link and echo the closing form tag, easy enough.
field1 [current data]
but rather
lastname [current data]
Furthermore, you certainly do not want to output all fields to the form, because then users would be able to modify primary keys and your whole DB would be screwed.
So first, I would grab the names of the fields, then build the form using that. Maybe that's more complicated than you want, but it has some advantages
- post array indexes will correspond to field names in the DB
- you'll have semi-meaningful labels for your fields.
- you'll be able to filter out certain fields by building an "exclude_field" array, which could go in your config file.
So, once again, we'll assume that your connection and all that is made in your config file and you've opened your form with the proper <form> tag higher up. We're now trying to get the individual fields. Now you need to get the field names.
$field_names = array();
$exclude_fields = array('id', 'last_updated');
$fields = mysql_list_fields("my_database", "my_table");
$num_fields = mysql_num_fields($fields);
for ($i = 0; $i < $num_fields; $i++)
{
$new_field = mysql_field_name($fields, $i);
if (!in_array($new_field, $exclude_fields)
{
$field_names[] = $new_field;
}
}
Now you have a list of all fields in your database, minus those fields that the user should not edit. You don't want them editing your primary key ('id') or the last update because you would set that with now(), so you don't want to output that to a form.
Now get your data from the table
$query = "SELECT * from my_table WHERE id=$id";
$result = mysql_query($query);
$data = mysql_fetch_array($result);
Now we get our data and put it in the form
foreach ($fieldnames as $field)
{
$form .= "<p>$field" . '<input type="text" name="field" value="{$row[$field]}">';
}
Now you add in your submit button and all that and close your form.
Caveats:
- not tested, just typed into the WebmasterWorld window
- you may need to do some checking to account for the cases when, say, the query returns no rows. Basic error control and so on.
PS - I see you wanted textarea, not input fields, but I assume that the change is clear enough.
Tom
$sql="show columns from table_name;
$result=mysql_query($sql);
while($row=mysql_fetch_assoc($result)){
if($row['Key']=="PRI")continue;//skip primary key, probably auto_increment
echo $row['Field'],"<br>\n"; //this is the column name to be used for your input field
}