如果没有新图像更新在PHP中如何将旧图片存储在数据库中

时间:2017-08-21 07:12:16

标签: php image

嗨,在我的表单中,当用户提交表单并选择无图像时,我的数据库更新为空白,你可以帮助我在代码中哪里有错误我没有PHP知识所以需要你的帮助

这是我的完整PHP代码,希望你得到它错在哪里

我想如果用户没有选择图像,那么旧图像不会替换为空白

我在我的代码中尝试我仍然空白!

<?php
include("connect.php");
    if(isset($_POST['submit']))
    {
        $id = $_POST['id'];
        $form_no = $_POST['form_no'];
        $shop_name = $_POST['shop_name'];
        $shop_address = $_POST['shop_address'];
        $owner_name = $_POST['owner_name'];
        $owner_address = $_POST['owner_address'];
        $phone = $_POST['phone'];
        $m_no = $_POST['m_no'];
        $email = $_POST['email'];
        $d_o_b = $_POST['d_o_b'];
        $d_o_m = $_POST['d_o_m'];
        $b_type = $_POST['b_type'];
        $reg_no = $_POST['reg_no'];
        $coment = $_POST['coment'];
        $date = date('y-m-d');
        $errors = "";
        $plan = $_POST['select'];
        $oldpic=$_POST["oldpic"];
        if($_FILES['image']['name'] != "" && $errors == "")
        {
            $file_name = $_FILES['image']['name'];
            $file_size = $_FILES['image']['size'];
            $file_tmp = $_FILES['image']['tmp_name'];
            $file_type = $_FILES['image']['type'];
            $file_ext=strtolower(end(explode('.',$file_name)));
            $expensions= array("jpeg","jpg","png");
            if(!in_array($file_ext,$expensions))
            {
                $errors="extension not allowed, please choose a JPEG or PNG file.";
            }

            if($file_size > 2097152)
            {
                $errors ='File size must be excately 2 MB';
            }
            if(empty($errors))
            {
                $new_file_name = uniqid().'.'.$file_ext;
                move_uploaded_file($file_tmp,"user/".$new_file_name);
            }
        }

        if(empty($errors))
        {
            $qreury = "UPDATE members SET 
            ref_no='$ref_no',shop_name='$shop_name', form_no='$form_no',
            shop_address='$shop_address', owner_name='$owner_name',
            owner_address='$owner_address', tel_no='$phone', mobile_no='$m_no',
            email='$email',dob='$d_o_b',marrige_date='$d_o_m',bussiness_type='$b_type',
            reg_no='$reg_no',extra_note='$coment',user_pic='$new_file_name',
            plan='$plan',target=(select price from members_plan where id ='$plan') WHERE id='$id'";
            $reslt = mysql_query($qreury)or die(mysql_error());
            header("location:registered_members.php");

        }
    elseif(empty($oldpic))
        {
            $qreury = "UPDATE members SET 
            ref_no='$ref_no',shop_name='$shop_name', form_no='$form_no',
            shop_address='$shop_address', owner_name='$owner_name',
            owner_address='$owner_address', tel_no='$phone', mobile_no='$m_no',
            email='$email',dob='$d_o_b',marrige_date='$d_o_m',bussiness_type='$b_type',
            reg_no='$reg_no',extra_note='$coment',user_pic='$oldpic',
            plan='$plan',target=(select price from members_plan where id ='$plan') WHERE id='$id'";
            $reslt = mysql_query($qreury)or die(mysql_error());
            header("location:registered_members.php");
        }
    }
?>

2 个答案:

答案 0 :(得分:0)

我理解你的问题,请让你的代码做它做的事情。即使你存储null,也不要担心。存储null并将avtar默认图像放在文件系统上,然后在获取图像时可以执行以下操作以便为您查看:

    <img src="
<?php !empty($your-image-with-directory)?$your-image-with-directory:"upload-directory/avtar.png"?>" width="100">

答案 1 :(得分:-2)

     {
if(empty($_FILES))   {
$error="Please select a photo to continue";
break;
}
    $reg_no = $_POST['reg_no'];
        $coment = $_POST['coment'];
        $date = date('y-m-d');
        $errors = "";
        $plan = $_POST['select'];
        $oldpic=$_POST["oldpic"];
        if($_FILES['image']['name'] != "" && $errors == "")
        {
            $file_name = $_FILES['image']['name'];
            $file_size = $_FILES['image']['size'];
            $file_tmp = $_FILES['image']['tmp_name'];
            $file_type = $_FILES['image']['type'];
            $file_ext=strtolower(end(explode('.',$file_name)));
            $expensions= array("jpeg","jpg","png");
            if(!in_array($file_ext,$expensions))
            {
                $errors="extension not allowed, please choose a JPEG or PNG file.";
            }

            if($file_size > 2097152)
            {
                $errors ='File size must be excately 2 MB';
            }
            if(empty($errors))
            {
                $new_file_name = uniqid().'.'.$file_ext;
                move_uploaded_file($file_tmp,"user/".$new_file_name);
            }
        }

        if(empty($errors))
        {
            $qreury = "UPDATE members SET 
            ref_no='$ref_no',shop_name='$shop_name', form_no='$form_no',
            shop_address='$shop_address', owner_name='$owner_name',
            owner_address='$owner_address', tel_no='$phone', mobile_no='$m_no',
            email='$email',dob='$d_o_b',marrige_date='$d_o_m',bussiness_type='$b_type',
            reg_no='$reg_no',extra_note='$coment',user_pic='$new_file_name',
            plan='$plan',target=(select price from members_plan where id ='$plan') WHERE id='$id'";
            $reslt = mysql_query($qreury)or die(mysql_error());
            header("location:registered_members.php");

            }
        elseif(empty($oldpic))
            {
                $qreury = "UPDATE members SET 
                ref_no='$ref_no',shop_name='$shop_name', form_no='$form_no',
                shop_address='$shop_address', owner_name='$owner_name',
                owner_address='$owner_address', tel_no='$phone', mobile_no='$m_no',
                email='$email',dob='$d_o_b',marrige_date='$d_o_m',bussiness_type='$b_type',
                reg_no='$reg_no',extra_note='$coment',user_pic='$oldpic',
                plan='$plan',target=(select price from members_plan where id ='$plan') WHERE id='$id'";
                $reslt = mysql_query($qreury)or die(mysql_error());
                header("location:registered_members.php");
            }
        }

        }while(0);

用我的代码替换你的代码