尝试在表单值字段中显示数据库值

时间:2014-04-08 16:21:53

标签: php forms

简要说明:当我从下拉列表中选择一个用户(从“用户”表中填充)时,我希望生成的页面在基于表单的视图中显示该用户的特定详细信息已根据我的数据库中的当前数据填写的值。数据库值未显示,而是在页面上显示$current_userlastname。代码如下:

if ($dbConnected) {
    $UserID = $_POST['UserID'];

$sql = "SELECT * FROM Users WHERE ID = '$UserID'";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    $current_userlastname = $row['User_LastName'];
    $current_userfirstname = $row['User_FirstName'];
    $current_useremail = $row['User_Email'];
    $current_username = $row['UserName'];
    $current_userpassword = $row['UserPassword'];
}

echo '<h2 style = "font-family: arial,helvetica,sans-serif;"> User EDIT </h2>';

echo '<form name="postUser" action="UserUpdate.php" method="POST">';
echo '<input type="hidden" name="UserID" value=".$UserID."/>';
echo '
        <table>
          <tr>
            <td>Last Name</td>
            <td><input type="text" name="User_LastName" value="$current_userlastname" /></td>
          </tr>
          <tr>
            <td>First Name</td>
            <td><input type="text" name="User_FirstName" value="$current_userfirstname" /></td>
          </tr>
          <tr>
            <td>Email Address</td>
            <td><input type="text" name="User_Email" value="$current_useremail" /></td>
          </tr>
          <tr>
            <td>Username</td>
            <td><input type="text" name="UserName" value="$current_username" /></td> 
          </tr>
          <tr>
            <td>User Password</td>
            <td><input type="text" name="UserPassword" value="$current_userpassword" /></td> 
          </tr>
          <tr>
            <td></td>
            <td align="right"><input type="submit" value="save" /></td>
          </tr>
        </table> 
        ';
echo '</form>';

3 个答案:

答案 0 :(得分:0)

您应始终使用mysqli_*代替mysql_*

<?php
$conn = mysqli_connect("hosta", "username", "password", "database");

$UserID = $_POST['UserID'];

$sql = "SELECT * FROM Users WHERE ID='$UserID'";
$query = mysqli_query($conn, $sql);

if ($row = mysqli_fetch_assoc($query)) { 
    $current_userlastname = $row['User_LastName'];
    $current_userfirstname = $row['User_FirstName'];
    $current_useremail = $row['User_Email'];
    $current_username = $row['UserName'];
    $current_userpassword = $row['UserPassword'];

echo '<h2 style = "font-family: arial,helvetica,sans-serif;"> User EDIT </h2>';

echo '<form name="postUser" action="UserUpdate.php" method="post">';
echo '<input type="hidden" name="UserID" value="'.$UserID.'"/>';
echo '<table>
          <tr>
            <td>Last Name</td>
            <td><input type="text" name="User_LastName" value="'.$current_userlastname.'" /></td>
          </tr>
          <tr>
            <td>First Name</td>
            <td><input type="text" name="User_FirstName" value="'.$current_userfirstname.'" /></td>
          </tr>
          <tr>
            <td>Email Address</td>
            <td><input type="text" name="User_Email" value="'.$current_useremail.'" /></td>
          </tr>
          <tr>
            <td>Username</td>
            <td><input type="text" name="UserName" value="'.$current_username.'" /></td> 
          </tr>
          <tr>
            <td>User Password</td>
            <td><input type="text" name="UserPassword" value="'.$current_userpassword.'" /></td> 
          </tr>
          <tr>
            <td></td>
            <td align="right"><input type="submit" value="save" /></td>
          </tr>
        </table>';
        echo '</form>';
}

答案 1 :(得分:0)

在单引号字符串中,不解析变量。

因此,如果你有一个变量$foo = "bar",那么

echo '$foo';

将打印$foo

但是

echo "$foo";

将打印bar

答案 2 :(得分:0)

不要对大量HTML使用'echo'。 PHP是一种'模板语言。当你想要将html发送到客户端时,当你想要进行处理时,你可以切换到它(&lt;?php)(?&gt;)。注意:&lt;?=是:&lt;?php echo。

的简称

我无法提供经过测试的代码,因为“mysql_ *”内容已被折旧。无论:

语法检查:

<!DOCTYPE HTML">
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF8">
    <title>Q22942773</title>
  </head>

  <body>
<?php
 /* debug - delete */ var_dump('_POST', $_POST, __FILE__, __LINE__, '<br />');
 /* debug - delete */ var_dump('dbConnected', $dbConnected, __FILE__, __LINE__, '<br />');
if ($dbConnected) {
    $UserID = $_POST['UserID'];

    $sql = "SELECT * FROM Users WHERE ID = '$UserID'";
    /* debug - delete */ var_dump('SELECT * FROM Users... ' $sql, __FILE__, __LINE__, '<br />');
    $result = mysql_query($sql);

    $row = mysql_fetch_array($result,MYSQL_ASSOC);
    /* debug - delete */ var_dump('row => ', $row, __FILE__, __LINE__, '<br />');
    if ($row) { // only expect one row so let the code show that rather than imply
                // more than one row with a while loop.
        $current_userlastname = $row['User_LastName'];
        $current_userfirstname = $row['User_FirstName'];
        $current_useremail = $row['User_Email'];
        $current_username = $row['UserName'];
        $current_userpassword = $row['UserPassword'];
    }
}
// drop out of PHP mode -- all the stuff after is sent to the client
// switch back into PHP mode to do processing of the $current_* variables
// note: <?= is short for: '<?php echo'
?>

<h2 style = "font-family: arial,helvetica,sans-serif;"> User EDIT </h2>

<form name="postUser" action="UserUpdate.php" method="POST">
<input type="hidden" name="UserID" value=".$UserID."/>
    <table>
      <tr>
        <td>Last Name</td>
        <td><input type="text" name="User_LastName" value="<?= $current_userlastname ?>" /></td>
      </tr>
      <tr>
        <td>First Name</td>
        <td><input type="text" name="User_FirstName" value="<?= $current_userfirstname  ?>" /></td>
      </tr>
      <tr>
        <td>Email Address</td>
        <td><input type="text" name="User_Email" value="<?= $current_useremail ?>" /></td>
      </tr>
      <tr>
        <td>Username</td>
        <td><input type="text" name="UserName" value="<?= $current_username?>" /></td>
      </tr>
      <tr>
        <td>User Password</td>
        <td><input type="text" name="UserPassword" value="<?= $current_userpassword ?>" /></td>
      </tr>
      <tr>
        <td></td>
        <td align="right"><input type="submit" value="save" /></td>
      </tr>
    </table>
</form>
</body>
</html>