创建表单以通过HTML表单更新特定行数据的部分工作。我已成功从mysql数据库中检索行数据但是当我尝试在html表单的字段中显示特定的行数据(StudentNumber,FirstName,LastName等)时,它只显示空字段。我可能错过了什么?
以下是显示表单的php页面的编码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>updating data record</title>
</head>
<body>
<?php
//connect to the database
$conn = mysql_connect('localhost', 'root', '');
if (!$conn) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('ccm3413', $conn);
mysql_set_charset("utf8",$conn);
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM studentinfo WHERE recordID = '$id'");
while ($row = mysql_fetch_assoc($result)) {
}
?>
<form id="form1" name="form1" method="post" action="processUpdateRecord.php">
<p>
<label>Student Number:
<input name="StudentNumber" type="text" id="StudentNumber" size="10" value="<?php echo $row['StudentNumber']; ?>"/>
</label>
</p>
<p>
<label>First Name:
<input name="FirstName" type="text" id="FirstName" size="20" value="<?php echo $row['FirstName']; ?>"/>
</label>
</p>
<p>
<label>Last Name:
<input name="LastName" type="text" id="LastName" size="20" value="<?php echo $row['LastName']; ?>"/>
</label>
</p>
<p>
<label>Email Address:
<input name="EmailAddr" type="text" id="EmailAddr" size="50" value="<?php echo $row['EmailAddr']; ?>"/>
</label>
</p>
<p>
<label>Telephone:
<input name="PhoneNumber" type="text" id="PhoneNumber" size="20" value="<?php echo $row['PhoneNumber']; ?>"/>
</label>
</p>
<p>
<input type="submit" name="Update" id="Update" value="Update" />
</p>
</form>
</body>
</html>
答案 0 :(得分:0)
请尝试使用此代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>updating data record</title>
</head>
<body>
<?php
//connect to the database
$conn = mysql_connect('localhost', 'root', '');
if (!$conn) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('ccm3413', $conn);
mysql_set_charset("utf8",$conn);
$id = mysql_real_escape_string($_GET['id']);
$result = mysql_query("SELECT * FROM studentinfo WHERE recordID = '$id'");
$student = (mysql_num_rows($result)==1) ? mysql_fetch_assoc($result) : null ; //You expect only 1 student, right?
if (is_array($student)){
?>
<form id="form1" name="form1" method="post" action="processUpdateRecord.php">
<p>
<label>Student Number:
<input name="StudentNumber" type="text" id="StudentNumber" size="10" value="<?php echo $student['StudentNumber']; ?>"/>
</label>
</p>
<p>
<label>First Name:
<input name="FirstName" type="text" id="FirstName" size="20" value="<?php echo $student['FirstName']; ?>"/>
</label>
</p>
<p>
<label>Last Name:
<input name="LastName" type="text" id="LastName" size="20" value="<?php echo $student['LastName']; ?>"/>
</label>
</p>
<p>
<label>Email Address:
<input name="EmailAddr" type="text" id="EmailAddr" size="50" value="<?php echo $student['EmailAddr']; ?>"/>
</label>
</p>
<p>
<label>Telephone:
<input name="PhoneNumber" type="text" id="PhoneNumber" size="20" value="<?php echo $student['PhoneNumber']; ?>"/>
</label>
</p>
<p>
<input type="submit" name="Update" id="Update" value="Update" />
</p>
</form>
<?php
else {
echo "The student has not been found" ;
} ?>
</body>
</html>
答案 1 :(得分:0)
您需要将表单放在while括号内。