dongzhankou2090 2014-04-08 16:21
浏览 36

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

Brief synopsis: When I select a user from a dropdown (which populates from Users table), I want the resulting page to show the specific details for that user in a form based view with the current values already filled in based on current data in my database. Database Values are not appearing, instead showing $current_userlastname on the page. Code below:

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条回答 默认 最新

  • douquanqiao6788 2014-04-08 16:30
    关注

    You should always use mysqli_* instead of 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>';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)