1

我使用列中的信息创建输入,例如,如果一个字段被调用about_content,它会输出一个带有输入字段的 About Content 标签。这适用于插入,但是我想以类似的方式使用此代码,UPDATE并且我想向用户显示在数据库中输入的字段的当前值。例如,如果about_content = Hello World!我希望输入值反映这一点。有没有办法动态地做到这一点?

<?php

require('dbc.php');

mysql_select_db($db);
$resultInput = mysql_query("SHOW COLUMNS FROM about WHERE Field NOT IN 
('id', 'created', 'date_modified', 'last_modified', 'update', 'type', 'bodytext') 
AND Field NOT REGEXP '_image'"); // selects only the columns I want

$result = mysql_query("SELECT * FROM about WHERE id=".$_GET['id']); // values I want to put into the values for <input>

while ($row = mysql_fetch_assoc ($result) && $column = mysql_num_rows ($resultInput)) {

    foreach($row as $column => $value){
        echo '<label>'.$column.'<input name="'.$column.'" type="input" value="'.$value.'"></label><br>';
    }
}
?>
4

3 回答 3

1

mysql_fetch_field

if (mysql_num_rows($result) > 0) {
//loop creates inputs
//make $resultInput object to array.
$i=0;
while ($row = mysql_fetch_assoc($result)) {
    $meta = mysql_fetch_field($result, $i);
    if(in_array($meta->name, $resultInput )){
    echo '<div class="wrapper"><label>' . ucfirst(str_replace('_', ' ',$meta->name)) . 
    '<br><input name="' . $meta->name . 
    '" type="text" class="input" value="$row[$meta->name]"><br></label></div>';
    }
    $i++;
 }
}
于 2012-07-01T16:03:25.870 回答
1

看到你用箭头标记的地方了吗?而不是 string (1),设置value为您读取的相应数据库值$result(而不是 in $resultInput)。

方法如下:mysql_fetch_assoc用于您的SELECT查询,而不是mysql_fetch_row. 只有一行,所以在开始生成表单之前获取它。然后,您将拥有一个包含行值的命名数组,您可以按名称获取每个字段并将其放入表单中。

如果您不明白如何执行此操作,请查看 php 文档mysql_fetch_assoc

$_GET['id']就像你在上一个问题中所说的那样逃避你。你乞求被pwned!

于 2012-07-01T15:28:55.747 回答
0
<?php
include('db.php');
if (isset($_POST['btn'])) {

    $dcolumn = $_POST['dyncolumns'];
    $dvalue = $_POST['dynfields'];

    $name = $_POST['name'];
    $mobile = $_POST['mobile'];
    $address = $_POST['address'];
    $query = "INSERT into addtable(name,mobile,address)VALUES('" . $name . "','" . $mobile . "','" . $address . "')";
    $result = mysql_query($query)or die(mysql_error());
    $id = mysql_insert_id();


    if ($dcolumn) {
        foreach ($dcolumn as $key => $value) {
            $result1 = "show COLUMNS from addtable like '.$value.'";
            $exists = mysql_query($result1)or die(mysql_error());
            $data = mysql_fetch_assoc($exists);

            if ($data==TRUE) {
                $query = "update addtable set $value = '" . $dvalue[$key] . "' where id=$id";
            } else {
                $query1 = "ALTER TABLE addtable ADD $value varchar(45)";
                $result2 = mysql_query($query1)or die(mysql_error());
                $query = "update addtable set $value = '" . $dvalue[$key] . "' where id=$id";
                $result = mysql_query($query)or die(mysql_error());
            }
        }
    }


}
?>
<script>
    function myFunction() {
        var table = document.getElementById("insert");
        var row = table.insertRow(3);
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        cell1.innerHTML = "<input type='text' name='dyncolumns[]'>";
        cell2.innerHTML = "<input type='text' name='dynfields[]' >";
    }
</script>
<html>
    <body>
        <form name="insert" method="post">

            <table border="2" align="center" id="insert">
                <tr>
                    <td>Name</td><td><input type="text" name="name" /></td>
                </tr>
                <tr>
                    <td>Mobile</td><td><input type="text" name="mobile" maxlength="10"/></td>
                </tr>
                <tr>
                    <td>Address</td><td><input type="text" name="address" /></td>
                </tr>

                <tr>
                    <td colspan="2" align="center"><input type="submit" name="btn" value="submit"></td>
                </tr>

            </table>
            <input type="button" onclick="myFunction()" name="add" value="Add">
            <a href="logout.php">Logout</a>
        </form>
    </body>
</html>
于 2017-04-05T10:14:37.867 回答