如何在不更改的情况下将空表单文件发送到文件列,将值更改为空

时间:2018-09-18 06:47:48

标签: php html

我有一个问题,如何在不更改值的情况下将空表单文件发送到文件列,
(当文件格式为空时,我已经在列中有一个我不想更改的图像。)

HTML:

<form method='post' enctype='multipart/form-data'>
  <input type="text" name="name">
  <input type='file' name='file'>
  <button name='Go'>Go</button>
</form>

PHP:

<?php
$C = new mysqli("Localhost","root","","anime_query");  
if(isset($_POST['Go'])) {
  $name = $_POST['name']);   
  $Img = addslashes(file_get_contents($_FILES['file']['tmp_name']));   
  $Q_S = "UPDATE anime_query SET name='$name',img='$Img' WHERE id=5";   
  $R_S = $C->query($Q_S);   
}
?>

1 个答案:

答案 0 :(得分:0)

首先,请使用准备好的语句(http://php.net/manual/en/pdo.prepared-statements.php),以确保在使用用户输入时不会被黑。 (http://php.net/manual/de/security.database.sql-injection.php

$sth = $dbh->prepare('UPDATE anime_query SET name=?,img=? WHERE id=5;');
$sth->execute(Array($name, $Img));

要检查是否上传了文件,可以使用以下命令:https://stackoverflow.com/a/946432/4339170

if(file_exists($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) { 
   // an image has been uploaded
   $sth = $dbh->prepare('UPDATE anime_query SET name=?,img=? WHERE id=5;');
   $sth->execute(Array($name, $Img));
} else {
   $sth = $dbh->prepare('UPDATE anime_query SET name=? WHERE id=5;');
   $sth->execute(Array($name));
}