dongyou8368 2018-05-18 10:13
浏览 27
已采纳

用户尝试更新时如何更新图像?

editform.php

   <?php
session_start();

if (!isset($_SESSION['id'])) {
    header('location:login.php');

}

?>

                        <?php
                        $nameErr = $emailErr = $usernameErr = $DateOfBirthErr = $departmentErr = $ageErr = $fileToUploadErr = $fileToUploadErrr = $fileToUploadErrrr = $fileToUploadErrrrr = $fileToUploadErrrrrr = "";
                        $name = $email = $username = $DateOfBirth = $department = $age  = $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 = $_FILES['filename']['name'];
                        echo "<img alt='image' style='width:180px;height:100px;' src='upload/" . $row['filename'] . "'>";


                            // checking weather the form has been submitted before performing the update query
                        if (isset($_POST['update'])) {

                            //Name Section    
                            if (empty($_POST["name"])) {
                                $nameErr = "Name is required";
                            } else {
                                $name = $_POST["name"];
                              // check if name only contains letters and whitespace
                                if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
                                    $nameErr = "Only letters and white space allowed";
                                }
                            }

                            //E-mail Section
                            if (empty($_POST["email"])) {
                                $emailErr = "Email is required";
                            } else {
                                $email = $_POST["email"];
                            // check if e-mail address is well-formed
                                if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                                    $emailErr = "Invalid email format";
                                }
                            }

                            //Username Section
                            if (empty($_POST["username"])) {
                                $usernameErr = "Username is required";
                            } else {
                                $username = $_POST["username"];
                             // check if name only contains letters and whitespace
                                if (!preg_match("/^[a-zA-Z ]*$/", $username)) {
                                    $usernameErr = "Only letters and white space allowed";
                                }
                            }

                            //D.O.B Section
                            if (empty($_POST["Date_of_birth"])) {
                                $DateOfBirthErr = "Date Of Birth is required";
                            } else {
                                $DateOfBirth = $_POST["Date_of_birth"];
                            }

                            //Department Section
                            if (empty($_POST["department"])) {
                                $departmentErr = "Department is required";
                            } else {
                                $department = $_POST["department"];
                            }

                            //Age Section
                            if (empty($_POST["age"])) {
                                $ageErr = "Age is required";
                            } else {
                                $age = $_POST["age"];
                            }


                            //UPLOAD FILE
                            $uploaddir = 'upload/';
                            $uploadfile = $uploaddir . basename($_FILES['filename']['name']);
                            $imageFileType = pathinfo($uploadfile, PATHINFO_EXTENSION);

                            if ($_FILES['filename']['name'] != '') {  
                                // Check the image is actaully image or not
                                $check = getimagesize($_FILES["filename"]["tmp_name"]);
                                if ($check == false) {
                                    $fileToUploadErrrrr = "File is not an image.";

                                } 

                                // Check if file already exists
                                if (file_exists($uploadfile)) {
                                    $fileToUploadErrrrrr = "Sorry, file already exists.";
                                }

                                if ($imageFileType != "jpg" && $imageFileType != "png") {
                                    $fileToUploadErr = "Sorry, only JPG,PNG files are allowed.";
                                } elseif ($_FILES['filename']['size'] > 500000) {

                                    $fileToUploadErrrr = "Sorry, your file is too large.";

                                } else {

                                  /*  if ($filename != '') {
                                        $target = "upload/" . $filename;
                                        unlink($target);
                                    }*/

                                    $uploaded = move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile);
                                    if ($uploaded) {
                                        $file = $_FILES['filename']['name'];
                                    } else {
                                        $file = $filename;
                                    }
                                }
                            }  else {
                                $file = $filename;
                            }


                            if ($nameErr == "" && $emailErr == "" && $usernameErr == "" && $DateOfBirthErr == "" && $departmentErr == "" && $ageErr == "" && $fileToUploadErr == "" && $fileToUploadErrr == "" && $fileToUploadErrrr == "" && $fileToUploadErrrrr == "" && $fileToUploadErrrrrr == "") {
                                $check = "SELECT * FROM users WHERE username = '$username' and userid !='$id'";
                                $rs = mysqli_query($mysqli, $check);
                                $da = mysqli_fetch_array($rs, MYSQLI_NUM);
                                if ($da > 0) {
                                    echo "Username Already in Exists";
                                } else {
                                    if($file=="")
                                    {
                                $sql = "update `users` set name='$name', username='$username', email='$email', Date_of_birth='$DateOfBirth', department='$department', age='$age' where userid ='$id'";
                                if (mysqli_query($mysqli, $sql)) {
                                        echo "Record updated successfully";
                                    } else {
                                        echo "Error updating record: " . mysqli_error(mysqli_query);
                                    }
                                    mysqli_close($mysqli);
                                    }
                                    else{

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

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

                        ?>


                        <!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="" enctype="multipart/form-data">
                            <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">
                            <span class="error"> <?php echo $nameErr; ?></span>
                            <br><br>

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

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

                            <label style="color:#0099ff">Date_of_birth: </label><input type="date" class= "copy1" value="<?= $DateOfBirth ?>" name="Date_of_birth">
                            <span class="error"> <?php echo $DateOfBirthErr; ?></span>
                            <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>
                            <span class="error"> <?php echo $departmentErr; ?></span>
                            <br><br>

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

                            <label style="color:#0099ff">Select image : </label><input type="file" style="width:180px;height:100px;padding:10px;" name="filename" >

                            <span class="error"><br> <?php echo $fileToUploadErr; ?></span>
                            <span class="error"><br> <?php echo $fileToUploadErrrr; ?></span>
                            <span class="error"><br> <?php echo $fileToUploadErrrrr; ?></span>
                            <span class="error"><br> <?php echo $fileToUploadErrrrrr; ?></span>


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

                    </div>

            </body>
            </html>

Hi everyone i really really need you people help. Actually i creating a registration form where users can register their details. The registration form is absolutely working well. and even the users when logged in thats also working good, but now when the users tries to update his details, such as name, username is getting updated but when the user tries to update his image then the problem arises, except image the remaining portion is getting updated when it comes to the image, the image is not getting update, please guys its my humble request to all the people who are viewing my question please help me to solve this issue, i'm trying to fix this from four days but im unable to do so please please please please help me i really need your help please

  • 写回答

2条回答 默认 最新

  • doudu5498 2018-05-18 10:43
    关注

    While editing edit image operation,

    you should put your update sql query with if else condition.

    So, basically,

    if(empty($image_file_name)){
    
    //query without image column
    
    $sql = "update `users` set name='$name',username='$username', 
    email='$email', Date_of_birth='$DateOfBirth', department='$department',
    age='$age' where userid ='$id'" ;
    
    //do not forget to execute query here with mysqli_query 
    
    }
    if(!empty($image_file_name)){
    
    //query with image column
    
    $sql = "update `users` set name='$name',username='$username', 
    email='$email', Date_of_birth='$DateOfBirth', department='$department',age='$age',
    image_name='$image_file_name' where userid ='$id'" ;
    
    //do not forget to execute query here with mysqli_query 
    
    }
    

    Also make sure we can not prefetch value like textbox, in input type='file', but yes, you can preview the already uploaded image with img tag.

    So, instead

    <input type='file' value='<?php echo $filename; ?>' <!--do not use this-->
    

    You should use,

    <input type='file'> 
    <img src='your_already_uploaded_file_path' height='50px' width='50px'> <!--change height width as you wish-->
    

    Also, make sure, you are missing enctype="multipart/form-data", with your form tag. So, do not forget to add this attribute with your form tag. This attribute is must while working with uploading files.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。