我目前正在尝试使用mysql/php构建表单,下面是到目前为止我拥有的代码的一部分
I am currently trying to build a form using mysql/php, below is part of the code I have so far
第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>到目前为止,这是给我的一种表单,其中使用字段名称生成标签和输入框,以显示字段值.我想使用jquery datepicker进一步格式化某些字段,但仅适用于mysql表中类型为Date的那些字段.
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.
我一直在尝试使用类似以下内容的mysqli_fetch_field_direct:
I've been trying mysqli_fetch_field_direct using something like:
第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:
假设我有一个名为email的字段,该字段的类型= varchar,并且我的SQL查询生成一个结果test@example
Let's say I have a field called email which has type = varchar and my SQL query generates one result test@example
从BLOCK#1我得到:
From BLOCK#1 I get:
------------------------------- Field-Name | Field-Value email | test@example从BLOCK#2我得到:
From BLOCK#2 I get:
------------------------------- Field-Name | Field-Type email | varchar我想要得到的是
------------------------------- Field-Name | Field-Type | Field-Value email | varchar | test@example这是因为我想使用字段类型将css类添加到输入框(例如使用datepicker).
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).
推荐答案我将输出放在表中,因为我无法入睡...
I put the output in a table because I can't sleep...
好吧...看看这是否是您想要的...
Okay... see if this is what you want...
这是我为另一个SO问题制作的表格:
This is a table I made for a different SO question:
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 php/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>所以...以澄清...
So... to clarify...
您可以在foreach中使用这些变量来输出或使用所需的信息:(例如,我将输出的第一行用于user_id)
You can use these variables in that foreach to output or use the information however you want: (I am using my first row of output, for the user_id, as an example)
-
$key是列/字段名称(例如user_id)
$key is the column/field name (such as user_id)
$field_types[$key]来自$result->fetch_field_direct($i)->type(例如3)
$mysql_data_type_hash[$datatype]是数据类型的字符串版本,使用代码顶部的$mysql_data_type_hash数组.这不是必需的,但我包括了它,因此此示例更加清楚. (例如int)
$mysql_data_type_hash[$datatype] is the string version of the datatype using the $mysql_data_type_hash array at the top of the code. This isn't necessary but I included it so this example is more clear. (such as int)
$proceso[$key] = $value =是该foreach语句(例如1)
$proceso[$key] = $value = is your value for this iteration of the foreach statement (such as 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更多推荐
如何获取字段类型和值?
发布评论