0

我已经到处搜索了几天,但似乎无法找到解决方案。

我正在用 PHP/mySQL 构建一个用户博客。

我想允许用户上传新的博客文章(也就是标题、博客和照片)。

一切正常,除了如果他们没有选择要上传的照片,脚本将不会解析,也不会将任何内容插入数据库。

我已经发布了解析脚本文件。

即使没有选择照片,如何在数据库中插入信息?

//Get photo details
?><?php 
if (isset($_FILES["blog_photo"]["name"]) && $_FILES["blog_photo"]["tmp_name"] != ""){
  $fileName = $_FILES["blog_photo"]["name"];
  $fileTmpLoc = $_FILES["blog_photo"]["tmp_name"];
  $fileType = $_FILES["blog_photo"]["type"];
  $fileSize = $_FILES["blog_photo"]["size"];
  $fileErrorMsg = $_FILES["blog_photo"]["error"];
  $kaboom = explode(".", $fileName);
  $fileExt = end($kaboom);
  list($width, $height) = getimagesize($fileTmpLoc);
  if($width < 10 || $height < 10){
    header("location: ../message.php?msg=ERROR: That image has no dimensions");
    exit(); 
  }

  //Restrict photo uploads
  $db_file_name = rand(100000000000,999999999999).".".$fileExt;
  if($fileSize > 3048576) {
    header("location: ../message.php?msg=ERROR: Your image file was larger than 3mb");
    exit();}    
  if (!preg_match("/\.(gif|jpg|png)$/i", $fileName) ) {
    header("location: ../message.php?msg=ERROR: Your image file was not jpg, gif or png type");
    exit();
  } else if ($fileErrorMsg == 1) {
    header("location: ../message.php?msg=ERROR: An unknown error occurred");
    exit();
  }

  $moveResult = move_uploaded_file($fileTmpLoc, "../user/$log_id/$db_file_name");
  if ($moveResult != true) {
    header("location: ../message.php?msg=ERROR: File upload failed");
    exit();
  }
  include_once("../php_includes/image_resize.php");
  $target_file = "../user/$log_id/$db_file_name";
  $resized_file = "../user/$log_id/$db_file_name";
  $wmax = 600;
  $hmax = 400;
  img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);

  //define variables from form to insert into database
  $bh =$_POST['blog_heading_a'];
  $bt =$_POST['blog_text_a'];
  $u =$_POST['user_id'];
  $a =$_POST['blog_photo'];

  //insert into database
  $insertSQL ="INSERT INTO blog (blog_heading, blog_photo, blog_day, blog_text, user_id) VALUES ('$bh','$db_file_name',NOW(),'$bt','$u')";
  mysql_select_db($database_database_conn, $database_conn);
  $Result1 = mysql_query($insertSQL, $database_conn) or die(mysql_error());
  header("location: ../user.php?u=$log_id");
  exit();
}
?>
4

1 回答 1

0

您需要在 外部进行数据库插入if(),因为该代码仅在上传照片后才会执行。

<?php 
if (isset($_FILES["blog_photo"]["name"]) && $_FILES["blog_photo"]["tmp_name"] != ""){
  $fileName = $_FILES["blog_photo"]["name"];
  $fileTmpLoc = $_FILES["blog_photo"]["tmp_name"];
  $fileType = $_FILES["blog_photo"]["type"];
  $fileSize = $_FILES["blog_photo"]["size"];
  $fileErrorMsg = $_FILES["blog_photo"]["error"];
  $kaboom = explode(".", $fileName);
  $fileExt = end($kaboom);
  list($width, $height) = getimagesize($fileTmpLoc);
  if($width < 10 || $height < 10){
    header("location: ../message.php?msg=ERROR: That image has no dimensions");
    exit(); 
  }

  //Restrict photo uploads
  $db_file_name = rand(100000000000,999999999999).".".$fileExt;
  if($fileSize > 3048576) {
    header("location: ../message.php?msg=ERROR: Your image file was larger than 3mb");
    exit();}    
  if (!preg_match("/\.(gif|jpg|png)$/i", $fileName) ) {
    header("location: ../message.php?msg=ERROR: Your image file was not jpg, gif or png type");
    exit();
  } else if ($fileErrorMsg == 1) {
    header("location: ../message.php?msg=ERROR: An unknown error occurred");
    exit();
  }

  $moveResult = move_uploaded_file($fileTmpLoc, "../user/$log_id/$db_file_name");
  if ($moveResult != true) {
    header("location: ../message.php?msg=ERROR: File upload failed");
    exit();
  }
  include_once("../php_includes/image_resize.php");
  $target_file = "../user/$log_id/$db_file_name";
  $resized_file = "../user/$log_id/$db_file_name";
  $wmax = 600;
  $hmax = 400;
  img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);

} else {
  $db_file_name = '';
}

//define variables from form to insert into database
$bh =$_POST['blog_heading_a'];
$bt =$_POST['blog_text_a'];
$u =$_POST['user_id'];
$a =$_POST['blog_photo'];

//insert into database
$insertSQL ="INSERT INTO blog (blog_heading, blog_photo, blog_day, blog_text, user_id) VALUES ('$bh','$db_file_name',NOW(),'$bt','$u')";
mysql_select_db($database_database_conn, $database_conn);
$Result1 = mysql_query($insertSQL, $database_conn) or die(mysql_error());
header("location: ../user.php?u=$log_id");
exit();

?>
于 2013-10-18T00:51:53.427 回答