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

发布于 2024-12-04 19:38:53 字数 2226 浏览 0 评论 0原文

我正在尝试使用 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();

?>

I am trying to populate a text field with data from a MySQL database, but my code seems to not be working.

Any help would be greatly appreciated.

Heres the code:

<?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();

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无需解释 2024-12-11 19:38:53

用完整的修复编辑了我的答案:

<?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();
?>

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

edited my answer with full fixes:

<?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();
?>

now replace whole coding with this one and reply...??

梦醒灬来后我 2024-12-11 19:38:53

只是一个猜测,但我认为数据库中的列名称将全部小写。但是在你的 php 中你有 $row["Contact"]。我认为应该是 $row["contact"]

Just a guess but I think your column names in the db would be all lower case. But in your php you have $row["Contact"]. I think it should be $row["contact"]

泪眸﹌ 2024-12-11 19:38:53

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

Make sure you use a querystring with a legitimate ID like example.com?ID=1. If you are posting a form and the post method for the form is post, you should use $_POST['ID'] instead of $_GET['ID'].

天邊彩虹 2024-12-11 19:38:53

This code:

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

should be like this

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

Also use

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

For debugging purpose.

删除mysql_close(),它应该起作用

This code:

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

should be like this

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

Also use

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

For debugging purpose.

Remove mysql_close() and it should work

白云悠悠 2024-12-11 19:38:53

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

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

这个网站的新用户,谢谢!

From previous comments, the id in your url is lower case change $_GET['ID'] to $_GET['id']

Im I allowed to do this? Keeps other people with problem from having to read all the comments

New to this site, thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文