3

I am currently trying to build a form using mysql/php, below is part of the code I have so far

BLOCK#1:

$proceso = mysqli_fetch_assoc($result); // my query returns only one row

...

<form action='actualizar.php' method='Post'>
<?php
foreach(array_keys($proceso) as $key){

echo "<label for='$key'>$key: </label>";
echo "<input name='$key' value='".$proceso[$key]."'><br/>";

}
echo "<input type='hidden' name='View' value='$view'>";
?>

<input type="submit" value="Actualizar">
</form>

This so far is getting me a form where I'm using the field names to generate labels and input boxes where i show the field value. I would like to further format some of the fields using the jquery datepicker, but only for those fields which have a type = Date in the mysql table.

I've been trying mysqli_fetch_field_direct using something like:

BLOCK#2:

$fields = mysqli_num_fields($result);
for ($i=0; $i < $fields; $i++) {
    $field_types[] = $result->fetch_field_direct($i)->type;
}

but in this case I can't get the value, just the type

Is there a straightforward way to get the type and value of a field?

Edited to (try) to simplify:

Let's say I have a field called email which has type = varchar and my SQL query generates one result test@example.com

From BLOCK#1 I get:

     -------------------------------
     Field-Name  | Field-Value
     email       | test@example.com

From BLOCK#2 I get:

     -------------------------------
     Field-Name | Field-Type
     email      | varchar

what I would like is to get

     -------------------------------
     Field-Name | Field-Type | Field-Value
     email      | varchar    | test@example.com

This is because I would like to use the field type to add a css class to the input box (such as to use the datepicker).

4

1 回答 1

7

编辑: 我把输出放在一张桌子上,因为我睡不着......

好吧……看看这是不是你想要的……

这是我为不同的 SO 问题制作的表格:

mysql> describe user;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| User_ID     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| Email       | varchar(100)     | YES  |     | NULL    |                |
| Name        | varchar(100)     | YES  |     | NULL    |                |
| Password    | varchar(100)     | YES  |     | NULL    |                |
| FB_ID       | int(11)          | YES  |     | NULL    |                |
| Total_Score | int(11)          | YES  |     | 0       |                |
| add_date    | datetime         | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

并从数据库:

mysql> select * from user limit 1;
+---------+-------+------+----------+-------+-------------+---------------------+
| User_ID | Email | Name | Password | FB_ID | Total_Score | add_date            |
+---------+-------+------+----------+-------+-------------+---------------------+ 
|       1 | NULL  | kim  | NULL     |  NULL |          10 | 2013-11-03 23:04:08 |
+---------+-------+------+----------+-------+-------------+---------------------+
+
1 row in set (0.00 sec)

和代码:

<?php
$mysqli = mysqli_connect("localhost", "root", "", "test");

// this came from http://php.net/manual/en/mysqli-result.fetch-field-direct.php 
$mysql_data_type_hash = array(
    1=>'tinyint',
    2=>'smallint',
    3=>'int',
    4=>'float',
    5=>'double',
    7=>'timestamp',
    8=>'bigint',
    9=>'mediumint',
    10=>'date',
    11=>'time',
    12=>'datetime',
    13=>'year',
    16=>'bit',
    //252 is currently mapped to all text and blob types (MySQL 5.0.51a)
    253=>'varchar',
    254=>'char',
    246=>'decimal'
);

// run the query... 
$result = $mysqli->query("select * from user limit 1"); 

// get one row of data from the query results 
$proceso = mysqli_fetch_assoc($result);

print "<table>
        <tr>
           <th>\$key</th>
           <th>\$value</th>
           <th>\$datatype</th>
           <th>\$dt_str</th>
        </tr>  ";

// to count columns for fetch_field_direct()
$count = 0; 

// foreach column in that row...
foreach ($proceso as $key => $value) 
{
  $datatype = $result->fetch_field_direct($count)->type;  
  $dt_str   = $mysql_data_type_hash[$datatype];
  $value    = (empty($value)) ? 'null' : $value;  

  print "<tr>
           <td>$key</td>
           <td>$value</td>
           <td class='right'>$datatype</td>
           <td>$dt_str</td>
         </tr>  ";  
  $count++; 
} 

print "</table>"; 

mysqli_close($mysqli);
?> 

<style>
   /* this is css that you don't need but i was bored so i made it pretty...! */
   table   { font-family:Courier New; 
             border-color:#E5E8E3; border-style:solid; border-weight:1px; border-collapse:collapse;}
   td,th   { padding-left:5px; padding-right:5px; margin-right:20px; 
             border-color:#E5E8E3; border-style:solid; border-weight:1px; }
   .right  { text-align:right }
</style>

所以……澄清一下……

您可以在其中使用这些变量foreach来输出或使用您想要的信息:(例如,我使用我的第一行输出,用于 ​​user_id)

  • $key是列/字段名称(例如user_id

  • $field_types[$key]来自$result->fetch_field_direct($i)->type(如3

  • $mysql_data_type_hash[$datatype]$mysql_data_type_hash是使用代码顶部的数组的数据类型的字符串版本。这不是必需的,但我将其包含在内,因此此示例更加清晰。(如int

  • $proceso[$key] = $value =是您对 foreach 语句的此迭代的值(例如1

输出:

$key           $value          $datatype      $dt_str
User_ID        1                       3      int
Email          null                  253      varchar
Name           kim                   253      varchar
Password       null                  253      varchar
FB_ID          null                    3      int
Total_Score    10                      3      int
add_date       2013-11-03 23:04:08    12      datetime
于 2013-11-04T04:49:45.737 回答