如何在编辑表单中显示上传的图像?

时间:2018-05-15 07:37:13

标签: php html css mysqli

我正在创建一个简单的php注册和登录表单。在这里我只有一个问题没有得到解决,当用户试图编辑他的信息时,它应该显示编辑表单中显示但除了图像部分的所有信息,当它重定向到编辑表单时应该显示文本以及注册时上传的图像。

任何人都可以告诉我如何在编辑表单中显示上传的图像,下面是我试过的代码

<?php

    $filename="";
    include_once 'connect.php';
    $id    =  $_GET['id'];
    $query = mysqli_query($mysqli,"select * from `users` where userid='$id'");
    $row   = mysqli_fetch_array($query);
    //set a default variable to hold the original value if $_POST is not triggered
    $name        = $row['name'];
    $username    = $row['username'];
    $email       = $row['email'];
    $DateOfBirth = $row['Date_of_birth'];
    $department  = $row['department'];
    $age         = $row['age'];
    $filename    = $row['filename'];

    // checking weather the form has been submitted before performing the update query
    if(!empty($_POST['hidden_id'])){
        $id          = $_POST['hidden_id'];
        $name        = $_POST['name'];
        $username    = $_POST['username'];
        $email       = $_POST['email'];
        $DateOfBirth = $_POST['Date_of_birth'];
        $department  = $_POST['department'];
        $age         = $_POST['age'];
        $filename    = $_POST['filename'];


        $sql = "update `users` set name='$name', username='$username', email='$email', Date_of_birth='$DateOfBirth', department='$department', age='$age' , filename='$filename' where userid='$id'" ;

        if (mysqli_query($mysqli, $sql)) {
            echo "Record updated successfully";
        } else {
            echo "Error updating record: " . mysqli_error(mysqli_query);
        } 
    }
?>


 <!DOCTYPE html>
<html>
<head>
    <style>
                    .error {
                        color:red;
                    }

                    .button {
                        background-color: #4CAF50;
                        border: none;
                        color: white;
                        padding: 10px 25px;
                        text-align: center;
                        text-decoration: none;
                        display: inline-block;
                        font-size: 16px;
                        margin: 4px 2px;
                        cursor: pointer;
                    }
                    .div1 {
                        background-color: #f2f2f2;
                        margin-top: -19px;
                        margin-bottom: -25px;
                        margin-left: -19px; 
                    }

                    .copy {
                        border-radius: 4px;
                        padding: 6px 20px;
                       border-style: ridge;
                    }

                    .copy1{
                         border-radius: 4px;
                         padding: 6px 28px;
                         border-style: ridge;
                    }

                    .copy2{
                         border-radius: 4px;
                         padding: 4px 2px;

                    }
    </style>
</head>
<body>

    <div style="padding-left: 250px" class="div1">
        <h2 style="color:#009999">EDIT :</h2>

<form method="POST" action="">
    <input type="hidden" name="hidden_id" value="<?= $id ?>"/>
    <label style="color:#0099ff">Name:</label><input class= "copy" style="margin-left: 52px" type="text" value="<?= $name ?>" name="name">
    <br><br>

    <label style="color:#0099ff">Username:</label><input type="text" class= "copy" style="margin-left:26px" value="<?= $username ?>" name="username">
    <br><br>

    <label style="color:#0099ff">E-mail:</label><input type="text" class= "copy" style="margin-left: 48px"  value="<?= $email ?>" name="email">
    <br><br>

    <label style="color:#0099ff">Date_of_birth:</label><input type="date" class= "copy1" value="<?= $DateOfBirth ?>" name="Date_of_birth">
    <br><br>

    <label style="color:#0099ff">Department:</label>
        <select name="department"  class= "copy2" style="margin-left:14px" value="<?= $department ?>">
            <option value="EE">Electrical & Electronics</option>
            <option value="EC">Electronics & Communication</option>
            <option value="ME">Mechanical</option>
            <option value="CS">Computer Science</option>
            <option value="CV">Civil</option>
            <option value="IS">Information Science</option>
        </select>
    <br><br>

    <label style="color:#0099ff">Age:</label><input type="text" class= "copy" style="margin-left:62px" value="<?= $age ?>" name="age">
    <br><br>

    <label style="color:#0099ff">Select image : </label><input type="file" name="filename" style="width:180px;height:100px;padding:10px;" value="<img alt='image' style='width:100px;height:50px;' src='upload/".$row['filename']."'>">
    <br><br>                                                                                                                                    

    <input type="submit" class="button" name="update" value="Update"> 
    <a href="welcome.php">Back</a>
</form>
    </div>
</body>
</html>

0 个答案:

没有答案