要获取字段名称及其值,并在php中编辑它

时间:2014-09-16 05:36:52

标签: php mysql

是否可以动态地从表中获取字段名称及其值(因为我必须从下拉列表中选择表格,因此我们无法将字段名称指定为字段名称可能从一个表到另一个表不同)并且值应该是可编辑的,以便可以更新......?如果有,怎么样? 这是我的代码。我已打印了该值但无法编辑,更新和格式化它..

$result = mysqli_query("select * from 2mcom where father_name='Siyaram'");
while (false != ($data = mysqli_fetch_array($result, MYSQLI_ASSOC)))
  foreach ($data as $key => $value)
  echo "$key: $value <br />";

这是我的表:

student_name  father_name  sub1  sub2

  kunwar        Siyaram    20     40

表格的输出值及其字段名称为:

Student_name : kunwar
father_name : Siyaram
sub1  : 20
sub2  : 40

3 个答案:

答案 0 :(得分:1)

使用DB_SERVER,DB_USER,DB_PASS,DB_NAME

的值

注意,您可以使用面向对象的mysqli样式。

$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASS,DB_NAME);
$result = $mysqli->query("select * from 2mcom where father_name='Siyaram'");

或者与您的代码一样的程序样式。但您必须包含$link,如下所示:

$link = mysqli_connect(DB_SERVER, DB_USER, DB_PASS,DB_NAME);
mysqli_query($link, "select * from 2mcom where father_name='Siyaram'")

其余代码: **更新以显示编辑**

<?php
mysqli_report(MYSQLI_REPORT_ALL);

/* Get field information for all columns */
while ($finfo = $result->fetch_field()) {
    $afinfo[] = $finfo;
}

?>
    <form action="YourForm.php" method="post" >
<?php
    $row = $result->fetch_assoc();
    foreach ($afinfo as $col){
?>
        <?php echo $col->name ?>: <input type="text" name="<?php echo $col->name ?>" value="<?php echo $row[$col->name] ?>"><br>
<?php      
    }    
?>
        <input type="submit" value="Send" >
    </form>

答案 1 :(得分:1)

当您使用以下查询提取下拉列表的表名时,可以为每个表提取列名称:

SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE table_name = 'tbl_name'
  [AND table_schema = 'db_name']
  [AND column_name LIKE 'wild']

答案 2 :(得分:1)

echo "<table>" ;
    $result = mysqli_query("select * from 2mcom where roll_no='138218600004'");
    $i = 0;
    while($row = mysqli_fetch_assoc($result)) {
        foreach($row as $key => $value) {
            $name = 'field';
            $id = $row['roll_no']; //rename this with your row id, this will give you identifiable input names
            echo '<tr><td>';
            echo $key . ': ';
            echo '</td>';
            echo '<form method="POST">';
            echo '<td>';

            echo '<input type="hidden" name="id[]" value="'. $row['roll_no'] .'"/>';
            echo '<input type="hidden" name="column[]" value="'. $key .'"/>';
            echo '<input type="text" name="field[]" value="'. $value .'"/>';

            echo '</td></tr>';
        }
        $i++;
    } 
      echo "<tr><td>";
       echo'<input type="submit" name="submit" value="submit"/>';
      echo "</tr></td>";
        echo'</form></table>';