繁体   English   中英

使用PHP从Mysql数据库填充数据的文本字段

[英]Populating Text Field with Data from Mysql Database with PHP

我正在尝试使用来自MySQL数据库的数据填充文本字段,但是我的代码似乎无法正常工作。

任何帮助将不胜感激。

这是代码:

<?php include ("../config.php");

// Connect to server and select database.
mysql_select_db($db, $con);

// get value of id that sent from address bar
$id = $_GET['ID'];


// Retrieve data from database
$sql="SELECT * FROM members WHERE id='$id'";
$result=mysql_query($sql);

echo '<form name="Edit" method="post" action="update.php">

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Name:</strong></td>
<td align="center"><strong>Contact:</strong></td>
<td align="center"><strong>Division:</strong></td>
<td align="center"><strong>Rank:</strong></td>
</tr>';
            while ($row = mysql_fetch_assoc($result))
            {
            //we will echo these into the proper fields

        echo '<tr>';
        echo '<td align="center"><input name="Name" type="text" id="Name" value="'; echo $row['Name']; echo '"></td>';
        echo '<td align="center"><input name="Contact" type="text" id="Contact" value="'; echo $row["Contact"]; echo'" size="15"></td>';
        echo'<td>
        <select name="Divison">
        <option value="L4D2">L4D2</option>
        <option value="CSS">CSS</option>
        <option value="GMOD">GMOD</option>
        <option value="TF2">TF2</option>
        </select>
        </td>
        <td>
        <select name="Rank">
        <option value="Division Leader">Division Leader</option>
        <option value="Admin">Administrator</option>
        <option value="Member">Member</option>
        </select>
        </td>
        </tr>';


echo '<tr>';
        echo '<td align="center"><input type="submit" name="Submit" value="Submit"></td>
        </tr>
</table>
</td>
</form>';
}
echo '
</tr>
</table>
</body>
</html>';


// close connection
mysql_close();

?>

完整修复了我的答案:

<?php include ("../config.php");

// Connect to server and select database.
mysql_select_db($db, $con);

// get value of id that sent from address bar
$id = $_GET['ID'];

// Retrieve data from database
$sql="SELECT * FROM members WHERE id='$id'";
$result=mysql_query($sql);

echo '<form name="Edit" method="post" action="update.php">
            <table width="400" border="0" cellspacing="1" cellpadding="0">
                <tr>
                    <td align="center"><strong>Name:</strong></td>
                    <td align="center"><strong>Contact:</strong></td>
                    <td align="center"><strong>Division:</strong></td>
                    <td align="center"><strong>Rank:</strong></td>
                </tr>';
while ($row = mysql_fetch_assoc($result)) {
    //we will echo these into the proper fields

    echo '<tr>
                <td align="center"><input name="Name" type="text" id="Name" value="'.$row['Name'].'"></td>
                <td align="center"><input name="Contact" type="text" id="Contact" value="'.$row["Contact"].'" size="15"></td>
                <td>
                    <select name="Divison">
                        <option value="L4D2">L4D2</option>
                        <option value="CSS">CSS</option>
                        <option value="GMOD">GMOD</option>
                        <option value="TF2">TF2</option>
                    </select>
                </td>
                <td>
                    <select name="Rank">
                        <option value="Division Leader">Division Leader</option>
                        <option value="Admin">Administrator</option>
                        <option value="Member">Member</option>
                    </select>
                </td>
            </tr>';
}
echo     '<tr>
                <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
            </tr>
           </table>
        </form>';

// close connection
mysql_close();
?>

现在用这个替换整个编码并回复... ??

只是一个猜测,但是我认为您在db中的列名称将全部为小写。 但是在您的php中,您有$ row [“ Contact”]。 我认为应该是$ row [“ contact”]

这段代码:

$sql="SELECT * FROM members WHERE id='$id'";

应该是这样的

$sql="SELECT * FROM members WHERE id='{$id}'";

也使用

$result=mysql_query($sql) or die(mysql_error());

用于调试目的。

删除mysql_close(),它应该可以工作

确保使用具有合法ID(例如example.com?ID=1)的查询字符串。 如果要发布表单,并且表单的post方法是post,则应使用$ _POST ['ID']而不是$ _GET ['ID']。

根据之前的评论,您网址中的ID为小写,将$ _GET ['ID']更改为$ _GET ['id']

我可以这样做吗? 让有问题的其他人不必阅读所有评论

这个网站的新手,谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM