Page 1 of 1

problem reading field names to form

Posted: Wed Mar 24, 2004 3:46 am
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.

Posted: Wed Mar 24, 2004 3:54 am
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 />';
    }
  }
}

Posted: Wed Mar 24, 2004 4:18 am
by Czar
I get empty result with that one... dunno.

Posted: Wed Mar 24, 2004 4:24 am
by markl999
Can you post exactly the code you are using now?

Posted: Wed Mar 24, 2004 5:49 am
by Czar
Cleared it already 8O . Thanks for help anyway.