Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果上传,则更新图像,否则使用先前存储的图像_Php_Database - Fatal编程技术网

Php 如果上传,则更新图像,否则使用先前存储的图像

Php 如果上传,则更新图像,否则使用先前存储的图像,php,database,Php,Database,您可以使用简单的if-else语句检查上载文件是否为空,并相应地重新格式化sql语句: <?php include '../db_conx.php'; $id = $_POST['id']; $file = addslashes(file_get_contents($_FILES["college_image"]["tmp_name"])); $insert_data1 = mysqli_real_escape_string($db_conx,$_POST['college

您可以使用简单的if-else语句检查上载文件是否为空,并相应地重新格式化sql语句:

<?php

include '../db_conx.php';

$id = $_POST['id'];   

$file = addslashes(file_get_contents($_FILES["college_image"]["tmp_name"]));


$insert_data1 = mysqli_real_escape_string($db_conx,$_POST['college_overview']);
$insert_data2 = mysqli_real_escape_string($db_conx,$_POST['college_courses']);
$insert_data3 = mysqli_real_escape_string($db_conx,$_POST['college_facilities']);

$test = "UPDATE college_details SET college_overview='$insert_data1', college_courses='$insert_data2', college_facilities='$insert_data3',college_image = '$file' where id = '$id' ";
if (mysqli_query($db_conx,$test)){
    header("Location:college.php?success");
}
{
    echo("Error description: " . mysqli_error($db_conx));
}
?>

您的代码易受sql注入攻击。用于更新数据。

使用简单的if-else语句检查$file。
if(!file_exists($_FILES["college_image"]["tmp_name"]) ||
 !is_uploaded_file($_FILES["college_image"]["tmp_name"])) {
   $test = "UPDATE college_details SET college_overview='$insert_data1',
              college_courses='$insert_data2', college_facilities='$insert_data3', 
              where id = $id ";
 //No file is uploaded, we skip updating the image.
}else{
   //Upload File is there, we update the image file in the database
     $test = "UPDATE college_details SET college_overview='$insert_data1',
              college_courses='$insert_data2', college_facilities='$insert_data3', 
              college_image = '$file' where id = $id ";
 }