0

我的部分任务是创建一个表单以通过 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>
4

2 回答 2

0

您需要将表单放在 while 括号内。

于 2013-02-18T00:39:11.597 回答
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>
于 2013-02-17T19:02:55.227 回答