problem reading field names to form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Czar
Forum Commoner
Posts: 58
Joined: Sun Dec 29, 2002 11:17 am

problem reading field names to form

Post by Czar »

I need my script to print an input box from each field in database so that they are named by db:s fieldnames and valued by field's values.

started it this way:

Code: Select all

<?php
mysql_select_db($database_dbconn, $dbconn);
$query_config_read = "SELECT * FROM config";
$config_read = mysql_query($query_config_read, $dbconn) or die(mysql_error());
$row_config_read = mysql_fetch_assoc($config_read);
$totalRows_config_read = mysql_num_rows($config_read);
$fields = mysql_num_fields($config_read);
?>
<form action="" method="post" name="config_form">

Code: Select all

<?php
for( $loop = 1; $loop <= $fields; $loop++ ) {
echo "<input type='text' name='' value=''>".$name."<br>"; 
}
?>
</form>

OK. This prints as many input boxes as there are fields in DB, but...
i need it to name those inputs too.

I thought it would go somethin like this:

Code: Select all

<?php
for( $read = 1; $read <= $fields; $read++ ) {
  $name = mysql_field_name($row_config_read, $read); 
}
?>
and then just echo it somehow inside prints of 1:st loop, but couldnt get it to work. I'd appreciate any kind of help...

Thanks.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You could do something like,

Code: Select all

$query_config_read = "SELECT * FROM config";
$config_read = mysql_query($query_config_read) or die(mysql_error());
if(mysql_num_rows($config_read)){
  while($row_config_read = mysql_fetch_assoc($config_read)){
    foreach($row_config_read as $key=>$val){
      echo $key.' <input type="text" name="'.$key.'" value="'.$val.'" /><br />';
    }
  }
}
Czar
Forum Commoner
Posts: 58
Joined: Sun Dec 29, 2002 11:17 am

Post by Czar »

I get empty result with that one... dunno.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Can you post exactly the code you are using now?
Czar
Forum Commoner
Posts: 58
Joined: Sun Dec 29, 2002 11:17 am

Post by Czar »

Cleared it already 8O . Thanks for help anyway.
Post Reply