使用 PHP 使用 Mysql 数据库中的数据填充文本字段
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
用完整的修复编辑了我的答案:
现在用这个替换整个编码并回复...?
edited my answer with full fixes:
now replace whole coding with this one and reply...??
只是一个猜测,但我认为数据库中的列名称将全部小写。但是在你的 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"]
确保使用具有合法 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'].
This code:
should be like this
Also use
For debugging purpose.
删除mysql_close(),它应该起作用
This code:
should be like this
Also use
For debugging purpose.
Remove mysql_close() and it should work
根据之前的评论,您的网址中的 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!