如果在更新记录

时间:2017-11-21 06:59:23

标签: php

我正在尝试更新数据库中的记录,如果未选择图像新图像,则默认图像应保留在数据库中...我想知道我在哪里做错了或任何其他可用的解决方案问题??

if(isset($_POST['Update'])) 
{

    $Name = $_POST['name'];// user name
    $Password = $_POST['password'];// user password
    $Email=$_POST['email'];
    $Role=$_POST['role'];
    $errorMSG="";

    $imgFile = $_FILES['fileToUpload']['name'];
    $tmp_dir = $_FILES['fileToUpload']['tmp_name'];
    $imgSize = $_FILES['fileToUpload']['size'];

    if($imgFile)
    {
        $target_dir = "admin images/";
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $uploadOk = 1;
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) 
        {
            if ($_FILES["fileToUpload"]["size"] >  2097152) 
                {

                $errorMSG.="Sorry, your file is too large. ";
                $uploadOk = 0;
                }
            else 
                {
                if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
                    {

                        unlink('admin images/'.$Image);
                        $fileToUpload=$_FILES["fileToUpload"]["name"];
                        mysqli_query($connection,"update managers set Name='$Name', Password='$Password', Email='$Email', Role='$Role', Image='$fileToUpload' ") or die( mysqli_error());
                        ?>
                        <script>
                        alert('Successfully Updated ...');
                        window.location.href='users.php';
                        </script>
                        <?php
                    }
                else 
                    $errorMSG.="Sorry! Record Not Updated ";
                }

        }   

        else
            $errorMSG.="Invalid Format. "
    }

else

mysqli_query($connection,"update managers set Name='$Name', Password='$Password', Email='$Email', Role='$Role' ") or die('Query 02 Error: Record Not updated ');
    ?>
                        <script>
                        alert('Successfully Updated ...');
                        window.location.href='users.php';
                        </script>
                        <?php
} ?>

1 个答案:

答案 0 :(得分:0)

我修改了您现有的代码,请查看以下内容:

<?php
if(isset($_POST['Update'])) 
{

    $Name = $_POST['name'];// user name
    $Password = $_POST['password'];// user password
    $Email=$_POST['email'];
    $Role=$_POST['role'];
    $errorMSG="";

   $imgFile = $_FILES['fileToUpload']['name'];
    $tmp_dir = $_FILES['fileToUpload']['tmp_name'];
    $imgSize = $_FILES['fileToUpload']['size'];

    if($imgFile)
    {

        $target_dir = "admin images/";
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $uploadOk = 1;
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

        if($imageFileType == "jpg" or $imageFileType == "png" or $imageFileType == "jpeg" or $imageFileType == "gif" ) 
        {

            if ($_FILES["fileToUpload"]["size"] >  2097152) 
                {

                $errorMSG.="Sorry, your file is too large. ";
                $uploadOk = 0;

                }
            else 
                {
                if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
                    {


                        unlink('admin images/'.$Image);
                        $fileToUpload=$_FILES["fileToUpload"]["name"];
                        mysqli_query($connection,"update managers set Name='$Name', Password='$Password', Email='$Email', Role='$Role', Image='$fileToUpload' ") or die( mysqli_error());
                        ?>
                        <script>
                        alert('Successfully Updated Image ...');
                        window.location.href='users.php';

                        </script>
                        <?php
                    }
                else{ 
                    $errorMSG.="Sorry! Record Not Updated ";
                }
                }

        }   

        else{
            $errorMSG.="Invalid Format. ";
        }
    }

else {

mysqli_query($connection,"update managers set Name='$Name', Password='$Password', Email='$Email', Role='$Role' ") or die('Query 02 Error: Record Not updated ');
    ?>
                        <script>
                        alert('Successfully Updated Data ...');
                        window.location.href='users.php';

                        </script>
                        <?php
} } ?>

还有一件事是没有变量$ Image取消链接,所以你的上一张图片在这种情况下不会取消链接。 您也需要发送以前的图像。