我无法直接从数据库中的数据将值输入到表单中

时间:2016-12-09 08:01:47

标签: php mysql

如何让表单输入中的值等于从我的数据库中获取的数据。当我试图从后超级全局检索值时,我得到了数组([john_doe] =>),即空值

<?php print_r($_POST); ?>
<form action="" method="post">
    <?php while($subs = mysqli_fetch_assoc($result)) { ?>
        <input type="" name="john_doe" value='<?php echo $subs["menu_name"]; ?>'>
        <input type="submit" value="sumbit">
    <?php } ?>
</form>

1 个答案:

答案 0 :(得分:0)

您的数据库提取会出现问题,您可以尝试使用以下代码

<?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $sql = "**YOUR QUERY**";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
           <input type="text" name="john_doe" value='<?php echo $row["menu_name"]              ;?>'>
           <input type="submit" value="sumbit">
    $conn->close();
    ?>