我已经在这里搜索了好几天,但似乎无法找到解决方案。
我正在使用PHP / mySQL构建用户博客。
我想允许用户上传新的博文(AKA。标题,博客和照片)。
一切都很完美,除非他们没有选择要上传的照片,否则脚本将无法解析,也不会将任何内容插入数据库。
我发布了解析脚本文件。
即使未选择照片,如何在数据库中插入信息?
//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();
}
?>
答案 0 :(得分: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();
?>