尝试更新客户端信息时出现问题

时间:2018-03-05 19:10:05

标签: php mysql pdo

我遇到的问题是,当我上传或更新客户端时,如果您不添加要上传的图片,它将自动关闭客户端配置文件。但如果我只是想更新他们的网站或座右铭,并点击更新,我不想再尝试再搜索他们的图像并上传它只是为了更新一些项目。关于它为什么这样做的任何想法?先感谢您。

继承我的代码:

 <?php
        //Gets The Users info when editing it.
    $stmt = $DB_con->prepare('SELECT * FROM sponsors WHERE id='.$id);
    $stmt->execute();
if($stmt->rowCount() > 0)
{
     $row=$stmt->fetch(PDO::FETCH_ASSOC);

        extract($row);
}

?>  





 <div class="col-xs-12 col-sm-6 col-md-8">

                  <form method="post" class="form-horizontal" enctype="multipart/form-data">
                                          <div class="form-group">
                                            <label for="sponsor_name" class="col-sm-3 control-label">Sponsor Name:</label>
                                            <div class="col-sm-9">
                                                <input type="text" class="form-control" name="sponsor_name" id="sponsor_name" value="<?php echo $name;?>">
                                            </div>
                                        </div>
                                          <div class="form-group">
                                            <label for="sponsor_phone" class="col-sm-3 control-label">Sponsor Phone Number:</label>
                                            <div class="col-sm-9">
                                                <input type="text" class="form-control" name="sponsor_phone" id="sponsor_phone" value="<?php echo $phone;?>">
                                            </div>
                                            </div>  
                                            <div class="form-group">
                                            <label for="sponsor_moto" class="col-sm-3 control-label">Sponsors Motto:</label>
                                            <div class="col-sm-9">
                                                <input type="text" class="form-control" name="sponsor_motto" id="sponsor_motto" value="<?php echo $motto;?>">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="sponsors_website" class="col-sm-3 control-label">Sponsors Website:</label>
                                            <div class="col-sm-9">
                                                <input type="text" class="form-control" name="sponsor_website" id="sponsor_website" value="<?php echo $website;?>">
                                            </div>
                                        </div>
                                            <div class="form-group">
                                            <label for="sponsor_on" class="col-sm-3 control-label">Sponsor on or off?:</label>
                                            <div class="col-sm-9">
                                                <input type="text" class="form-control" name="sponsor_on" id="sponsor_on" value="<?php echo $live;?>">
                                            </div>
                                        </div>

                                  <div class="form-group">

                                <label for="image" class="col-sm-3 control-label"> Profile Picture </label>
                                <div class="col-sm-9">                              
                                <p><img id="image" src="../images/sponsors/<?php echo $row['logo'];?>" height="150" width="150" /></p>
                                    <div class="col-sm-9">
                                    <input class="input-group" type="file" name="user_image" accept="image/*" />
                                    </div>          
<br>
<br>                                    
                                      <div class="col-sm-offset-3 col-sm-9">
 <button type="submit" name="btn_save_updates" class="btn btn-info waves-effect waves-light">Update Sponsor</button>                            
  <a  href="blue/deletesponsor.php?delete_id=<?php echo $id; ?>" /><button type="button" class="btn btn-outline btn-rounded btn-danger" title="click for delete" onclick="return confirm('Are You Sure You Want To Delete This Sponsor?')">Delete Sponsor</button></a>
                                     </div>


                                            </div>
                                        </div>
                                    </form>

这是我将更新数据库的PHP代码。

 <?php

    if(isset($_GET['edit_id']) && !empty($_GET['edit_id']))
    {
        $id = $_GET['edit_id'];
        $stmt_edit = $DB_con->prepare('SELECT * FROM sponsors WHERE id =:uid');
        $stmt_edit->execute(array(':uid'=>$id));
        $edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
        extract($edit_row);
    }
    else
    {
        header("Location: ../../login.php");
    }
    if(isset($_POST['btn_save_updates']))
    {
        $username = $_POST['user_name'];
        $description = $_POST['description'];       
        $imgFile = $_FILES['user_image']['name'];
        $tmp_dir = $_FILES['user_image']['tmp_name'];
        $imgSize = $_FILES['user_image']['size'];
        if($imgFile)
        {
            $upload_dir = '../images/sponsors/';
            $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
            $valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
            $userprofile = rand(1000,1000000).".".$imgExt;
            if(in_array($imgExt, $valid_extensions))
            {           
                if($imgSize < 5000000) 
                {
                    unlink($upload_dir.$edit_row['logo']);
                    move_uploaded_file($tmp_dir,$upload_dir.$userprofile);
                }
                else
                {
                    $errMSG = "Sorry, Your File Is Too Large To Upload. It Should Be Less Than 5MB.";
                }
            }
            else
            {
                $errMSG = "Sorry, only JPG, JPEG, PNG & GIF Extension Files Are Allowed.";      
            }   
        }
        else
        {
            $userprofile = $edit_row['userprofile'];
        }
        if(!isset($errMSG))
        {
            $sponsorname = $_POST['sponsor_name'];
            $motto = $_POST['sponsor_motto'];
            $phone = $_POST['sponsor_phone'];
            $website = $_POST['sponsor_website'];
            $son = $_POST['sponsor_on'];

            $stmt = $DB_con->prepare('UPDATE sponsors SET name=:sname, motto=:smotto, phone=:sphone,website=:swebsite,live=:son, logo=:upic WHERE id=:uid');
            $stmt->bindParam(':sname',$sponsorname);
            $stmt->bindParam(':smotto',$motto);
            $stmt->bindParam(':sphone',$phone);
            $stmt->bindParam(':swebsite',$website);
            $stmt->bindParam(':son',$son);
            $stmt->bindParam(':upic',$userprofile);
            $stmt->bindParam(':uid',$id);

            if($stmt->execute()){
                ?>
                <script>
                alert('Successfully Updated...');
                window.location.href='managesponsors.php?action=sponsorupdated';
                </script>
                <?php
            }
            else{
                $errMSG = "Sorry User Could Not Be Updated!";
            }
        }           
    }
?>

0 个答案:

没有答案