图像重新选择php

图像重新选择php,php,mysql,html,Php,Mysql,Html,我有一个表格,每行有一个图像和一些文本。当前,当我更新内容而不选择图像时,图像的数据库字段将被清除。但是,如果没有选择图像,我希望保留旧图像 我怎样才能做到这一点 请注意,我知道mysql_*函数已被弃用 <?php include("db/db.php"); $select_db = "select * from aboutus WHERE id=1"; $run_news = mysql_query($select_db); while ($row = mysql_fetch_a

我有一个表格,每行有一个图像和一些文本。当前,当我更新内容而不选择图像时,图像的数据库字段将被清除。但是,如果没有选择图像,我希望保留旧图像

我怎样才能做到这一点

请注意,我知道mysql_*函数已被弃用

<?php

include("db/db.php");

$select_db = "select * from aboutus WHERE id=1";
$run_news = mysql_query($select_db);

while ($row = mysql_fetch_array($run_news)) {
    $id = $row['id'];
    $image = $row['image'];
    $content = $row['content'];
}
?>
<div class="box box-primary">
    <div class="box-header with-border">
        <h3 class="box-title">Update About Content</h3>
    </div>
    <!-- /.box-header -->
    <!-- form start -->
    <form role="form" method="post" action="aboutcontent.php?id=1"  enctype="multipart/form-data">
        <div class="box-body">

            <div class="form-group">
                <label for="exampleInputFile">Reselect Image *(H=530px, W=800px)</label>
                <input type="file" name="user_image"  id="exampleInputFile">
            </div>

            <div class="form-group">
                <label >Content</label><br>
                <textarea name="content" class="tinymce" class="form-control" rows="15"><?php echo $content; ?></textarea>
            </div>
        </div>
        <!-- /.box-body -->

        <div class="box-footer">
            <button type="submit" name="update" class="btn btn-primary">Update</button>
        </div>
    </form>
</div>

<?php

include("db/db.php");

// Code for UPDATE button
if (isset($_POST['update'])) {

    $content = $_POST['content'];

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

    if ($imgFile) {
        $upload_dir = 'images/about/'; // upload directory  
        $imgExt = strtolower(pathinfo($imgFile, PATHINFO_EXTENSION)); // get image extension
        $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
        $userpic = rand(1000, 1000000) . "." . $imgExt;

        if (in_array($imgExt, $valid_extensions)) {
            if ($imgSize < 5000000) {
                unlink($upload_dir . $row['image']);
                move_uploaded_file($tmp_dir, $upload_dir . $userpic);
            }
            else {
                $errMSG = "Sorry, your file is too large it should be less then 5MB";
            }
        }
        else {
            $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        }
    }
    else {
        // if no image selected the old image remain as it is.
        $userpic = $row['image']; // old image from database
    }

    // if no error occured, continue ....
    $sql = "UPDATE aboutus SET image='$userpic', content='$content' WHERE id=1";
    $query = mysql_query($sql);
    if (!$query) {
        die('Invalid query: ' . mysql_error());
    }
    else {
        echo "<script>alert('Successfully Updated!!!'); window.location='index.php?aboutcontent'</script>";
    }
}

?>

内容更新
重新选择图像*(H=530像素,W=800像素)
内容
更新
由于我还不能发表评论,当你提交时没有图片,你是不是在if或else语句中(在每个部分放置一个骰子(“某些内容”)来解决这个问题)。如果您没有到达else,请尝试:

//initialize error message
$errMSG = '';

//this error means 'There is no error, the file uploaded with success'
if($_FILES['user_image']['error'] == 0){
 //rest of logic to get the filename and move files stuff
}
else{
   $userpic = $row['image'];
}

//you dont want to update database if there are errors
if(errMSG = ''{
  $sql = "UPDATE aboutus SET image='$userpic', content='$content' WHERE id=1";
}
或者只进行两个不同的数据库调用,一个用于有图像时,另一个用于没有图像时

if($_FILES['user_image']['error'] == 0){
 //rest of logic to get the filename and move files stuff
    //if everything else is true (has filename and correct file size)

   //you dont want to update database if there are errors
   if(errMSG = ''{
      $sql = "UPDATE aboutus SET image='$userpic', content='$content' WHERE id=1";
}
  $sql = "UPDATE aboutus SET image='$userpic', content='$content' WHERE id=1";
}
else{
   $userpic = $row['image'];

  $sql = "UPDATE aboutus SET content='$content' WHERE id=1";
}

由于我还不能发表评论,当你提交时没有图片,你是不是在if或else语句中(在每个部分放置一个骰子(“某些内容”)来解决这个问题。如果您没有到达else,请尝试:

//initialize error message
$errMSG = '';

//this error means 'There is no error, the file uploaded with success'
if($_FILES['user_image']['error'] == 0){
 //rest of logic to get the filename and move files stuff
}
else{
   $userpic = $row['image'];
}

//you dont want to update database if there are errors
if(errMSG = ''{
  $sql = "UPDATE aboutus SET image='$userpic', content='$content' WHERE id=1";
}
或者只进行两个不同的数据库调用,一个用于有图像时,另一个用于没有图像时

if($_FILES['user_image']['error'] == 0){
 //rest of logic to get the filename and move files stuff
    //if everything else is true (has filename and correct file size)

   //you dont want to update database if there are errors
   if(errMSG = ''{
      $sql = "UPDATE aboutus SET image='$userpic', content='$content' WHERE id=1";
}
  $sql = "UPDATE aboutus SET image='$userpic', content='$content' WHERE id=1";
}
else{
   $userpic = $row['image'];

  $sql = "UPDATE aboutus SET content='$content' WHERE id=1";
}

通过检查是否提交了文件,可以轻松解决此问题:

if(!empty($userpic)){
    // SQL update here
} else {
    // No file submitted so don't update
}

获取空mysql字段的原因是,您正在使用空变量更新字段。

通过检查是否提交了文件,可以轻松解决此问题:

if(!empty($userpic)){
    // SQL update here
} else {
    // No file submitted so don't update
}


您获取空mysql字段的原因是您正在使用空变量更新字段。

@Fred ii-您有解决方案吗?“我知道它已被弃用”。那为什么不更新呢?现在,您要求我们帮助您维护一个非常不安全的脚本。一个脚本,它不仅让你处于危险之中,也让你的用户处于危险之中。我们不会告诉你这些,因为我们不想帮你解决你的问题。我们这么说是因为我们不想让你稍后再回来,因为你遇到了一些严重的麻烦。你对你的用户负责你知道的私人数据。人们可以起诉你,如果你没有尽力保护这些数据,你就会有罪@icecub thansk对于你为我写这篇短文所花的时间,我可以开始用mysqli或oop写这篇文章,因为我知道我想知道的是问题在哪里问题很简单。您总是在更新数据库中的图像字段。相反,只需在执行更新之前检查文件是否已提交<代码>如果(!empty($userpic)){//SQL update here}否则{//没有提交文件,所以不要更新}
@icecub这就是我想要的!谢谢,我不知道为什么我不去想这个,把这个作为答案,这样我就可以把它标记为accepted@Fred-你有解决办法吗?“我知道它已经过时了”。那为什么不更新呢?现在,您要求我们帮助您维护一个非常不安全的脚本。一个脚本,它不仅让你处于危险之中,也让你的用户处于危险之中。我们不会告诉你这些,因为我们不想帮你解决你的问题。我们这么说是因为我们不想让你稍后再回来,因为你遇到了一些严重的麻烦。你对你的用户负责你知道的私人数据。人们可以起诉你,如果你没有尽力保护这些数据,你就会有罪@icecub thansk对于你为我写这篇短文所花的时间,我可以开始用mysqli或oop写这篇文章,因为我知道我想知道的是问题在哪里问题很简单。您总是在更新数据库中的图像字段。相反,只需在执行更新之前检查文件是否已提交<代码>如果(!empty($userpic)){//SQL update here}否则{//没有提交文件,所以不要更新}@icecub这就是我想要的!谢谢,我不知道为什么我不这么想,把这个作为一个答案,这样我就可以把它标记为接受仍然没有发生任何事情添加一个新的答案,这样我就可以给你评分,这样你就可以开始评论你要达到的条件的哪一部分了?您是否总是在if部分或else语句中结束,其中make image=数据库值是什么表示整个代码。每次我更新['content']时,我都必须重新设置图像,或者数据库中的['image']字段变为空。你能
die(var_dump(_FILES))在你之后如果(设置($\u POST['update']),这样我们就可以看到在没有选择照片时发送的内容仍然没有发生任何事情添加一个新的答案,这样我就可以给你评分,这样你就可以开始评论你的条件的哪一部分了?您是否总是在if部分或else语句中结束,其中make image=数据库值是什么表示整个代码。每次我更新['content']时,我都必须重新设置图像,或者数据库中的['image']字段变为空。你能
die(var_dump(_FILES))在你之后如果(设置($\u POST['update']),这样我们就可以看到没有照片时发送的内容selected@SaqibMalik不是真的。这只是一些基本的东西,哈哈。我建议你阅读和使用这门课:你大概需要15分钟才能理解和使用它。这将使你的生活变得更加轻松:)已经得到了:我想知道的是我怎样才能做到这一点mysql@SaqibMalik我懂了。我很高兴你现在已经解决了。祝你好运和快乐@SaqibMalik不是真的。这只是一些基本的东西,哈哈。我建议你阅读和使用这门课:你大概需要15分钟才能理解和使用它。这将使你的生活变得更加轻松:)已经得到了:我想知道的是我怎样才能做到这一点mysql@SaqibMalik我懂了。我很高兴你现在已经解决了。祝你好运和快乐!