[英]Populating Text Field with Data from Mysql Database with PHP
I am trying to populate a text field with data from a MySQL database, but my code seems to not be working. 我正在尝试使用来自MySQL数据库的数据填充文本字段,但是我的代码似乎无法正常工作。
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();
?>
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...?? 现在用这个替换整个编码并回复... ??
Just a guess but I think your column names in the db would be all lower case. 只是一个猜测,但是我认为您在db中的列名称将全部为小写。 But in your php you have $row["Contact"]. 但是在您的php中,您有$ row [“ Contact”]。 I think it should be $row["contact"] 我认为应该是$ row [“ contact”]
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 删除mysql_close(),它应该可以工作
Make sure you use a querystring with a legitimate ID like example.com?ID=1. 确保使用具有合法ID(例如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']. 如果要发布表单,并且表单的post方法是post,则应使用$ _POST ['ID']而不是$ _GET ['ID']。
From previous comments, the id in your url is lower case change $_GET['ID'] to $_GET['id'] 根据之前的评论,您网址中的ID为小写,将$ _GET ['ID']更改为$ _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! 这个网站的新手,谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.